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';
?>
I’m not sure why, but for some reason you have to use
include()
orrequire()
insideform()
, instead ofinclude_once()
andrequire_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()
andrequire_once()
will work fine insidewidget()
, but it’s still better to useinclude()
andrequire()
in case the widget has multiple instances.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.