Display single widget

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

Read More

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.

Related posts

1 comment

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

Comments are closed.