Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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';
?>
share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.