File included into functions.php via require_once() won’t echo?

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.

Read More

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';
?>

Related posts

Leave a Reply

2 comments

  1. 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.

  2. If you ever resolved this, please share the answer for others. One potential reason for what you were experiencing is if you were in a child theme. TEMPLATEPATH references the parent theme and STYLESHEETPATH references the child theme.