Blog |Follow Nick on Twitter| About
 

So WP2.5 is out, and I figured it was about time I squashed that load bug on phpbb_recent_topics, while I'm at it I figure I'll wigetize it.

The problem is that the example on the automattic site doesn't actually work! If you paste...

function widget_myuniquewidget($args) {
    extract($args);
?>
        <?php echo $before_widget; ?>
            <?php echo $before_title
                . 'My Unique Widget'
                . $after_title; ?>
            Hello, World!
        <?php echo $after_widget; ?>
<?php
}
register_sidebar_widget('My Unique Widget','widget_myuniquewidget');

Into a blank plugin you get this in your logs...

PHP Fatal error:  Call to undefined function:  register_sidebar_widget()

Now I've not been bothered to dig out the exact reason why yet, something to do with the sidebar loading, but you need to wrap the whole lot up in an init function, so try this instead...

function widget_init_myuniquewidget() {
    // Check for required functions
    if (!function_exists('register_sidebar_widget'))
        return;

    function widget_myuniquewidget($args) {
        extract($args);
    ?>
            <?php echo $before_widget; ?>
                <?php echo $before_title
                    . 'My Unique Widget'
                    . $after_title; ?>
                Hello, World!
            <?php echo $after_widget; ?>
    <?php
    }
}

// Delay plugin execution until sidebar is loaded
add_action('widgets_init', 'widget_init_myuniquewidget');

I just need to work out now, how to add wiget options to the wp-admin panel and the next verions of my plugin will be done :)

rgds,

Nick

 
Nick Bettison ©