I’m trying to load a file via require_once() to handle some enhanced functionality in my theme. I could place all the code directly inside functions.php, but I’m trying to isolate this code into its own script.

The file is widget_context.php (see code below) and right now I’m just trying to echo some text back to each widget. It works fine when I do the echo directly from the function that calls the file.

However, I can’t get the echo or return (from the widget_context.php file) to show up in the widget.

What am I missing?

This function is inside functions.php

add_filter('in_widget_form', 'widget_context');

function widget_context(){
    //echo works here:
    //echo 'in widget form'; 
    $widgets_context_file = TEMPLATEPATH . "/widget_context.php";
    require_once($widgets_context_file);
}

Here is the contents of widget_context.php

<?php
    echo 'place the checkboxes here';
    return 'place the checkboxes here';
?>

2 Answers
2

I’m not sure why, but for some reason you have to use include() or require() inside form(), instead of include_once() and require_once(). I’m guessing that form() is being called multiple times, and so the external view file only gets included the first time. include_once() and require_once() will work fine inside widget(), but it’s still better to use include() and require() in case the widget has multiple instances.

Leave a Reply

Your email address will not be published. Required fields are marked *