1

I've created a simple one-off widget to let the admin change some copy on the home page, but the widget isn't showing its contents.

On the home page, I have this code: <?php the_widget('home_widget'); ?>

And this is the source code for the widget itself, as it appears in my functions.php file (it's Jeff Starr's Clean Markup Widget):

// Clean Markup Widget @ http://perishablepress.com/clean-markup-widget/
add_action('widgets_init', create_function('', 'register_widget("home_widget");'));
class home_widget extends WP_Widget {
    function __construct() {
        parent::WP_Widget('home_widget', "home page box",
                          array('description'=>"The contents of the home page box"));
    }
    function widget($args, $instance) {
        extract($args);
        $markup = $instance['markup'];
        if ($markup) echo $markup;
    }
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['markup'] = $new_instance['markup'];
        return $instance;
    }
    function form($instance) {
        if ($instance) $markup = esc_attr($instance['markup']);
        else $markup = __('Sample markup', 'markup_widget'); ?>
        <p>
            <label for="<?php echo $this->get_field_id('markup'); ?>"><?php _e('Markup/text'); ?></label><br />
            <textarea class="widefat" id="<?php echo $this->get_field_id('markup'); ?>" name="<?php echo $this->get_field_name('markup'); ?>" type="text" rows="16" cols="20" value="<?php echo $markup; ?>"><?php echo $markup; ?></textarea>
        </p>
<?php }
}

The widget looks and works perfectly in the Admin tool, and I can add text and save it, etc. But nothing displays on the home page. How can I get it to display the widget contents?

To be clear: while I am putting the widget into a sidebar on the Admin interface (because you have to), I don't want to display the sidebar (since I have other widgets in there). I just want this one-off widget to display its contents on the home page. Very simple.

2

You can force a particular widget that you have modified on the backend to show up if you can identify is sufficiently. I consider that very prone to error.

You should be able to get the_widget to work if you pass enough detail through the (up to) three parameters-- like this:

the_widget('home_widget',array('markup' => 'Yay')); 

But you don't get to configure that from the backend.

I would suggest that if you are going to use a widget, create a sidebar for the widget.

  • Ok, I see now what the_widget does... thank you. Sounds like widgets are really built to live in sidebars and they don't like being outside of them. :-) Could I ask you to look at this question again? It's what led me to widgets in the first place. wordpress.stackexchange.com/questions/103433/… – Eric Jun 21 '13 at 20:29
  • @Michelle : That is better posted as a comment to the question than as a comment to this answer. – s_ha_dum Jul 15 '13 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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