Call to undefined function: register_sidebar_widget

line Tags: , , ,

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 :)

nick

 

3 Comments

  1. Daniel J. Summers Says:

    Thanks for posting this. I had the exact same problem, and your solution also worked for me! :)

  2. pingback from: Call to undefined function: register_sidebar_widget « WordPress SOS
  3. Daniel Rosenstark Says:

    Thanks to you and Google for bringing me here, that worked. This is what I love about open source: when there is good documentation, like in this case (http://codex.wordpress.org/Plugins/WordPress_Widgets_Api), it gets out of date and instead of becoming useless, it just gets confusing.

    Thanks again!

Got something to say?

 

Some other things that might interest you...

---