Working on a theme made by someone else as an official duty. At first it’s causing error in the wp-admin Widgets page — not working at all. Then the first thought came to my mind was debugging…
Debugging steps
» Activated WP_DEBUG
.
» Deactivated all the plugins.
» Switched to default theme ‘T13’.
Errors
Found that the problem was from the theme. Activated the theme again, and it’s showing many DEBUG notices both in front-end and back-end. First of ’em was:
register_sidebar_widget()
is deprecated.
So without hesitation I went to wp_register_sidebar_widget()
. But found myself in a claustrophobic arena — I exactly don’t know how to register widgets with wp_register_sidebar_widget
. But with Codex and other WP.SE.com threads, I tried the following:
<?php
// Register widgetized areas
function theme_widgets_init() {
// Area 1
wp_register_sidebar_widget(
'primary_widget_area', //widget_id/ widget_slug
'Primary Widget Area', //widget_name
'primary_widget_display', //callback function
array(
'description' => 'The primary widget on the right side'
)
);
function primary_widget_display( $args ) {
extract($args);
echo $before_widget;
echo $before_title . 'My Unique Widget' . $after_title;
echo $after_widget;
// print some HTML for the widget to display here
echo "Your Widget Test";
}
// Area 2
wp_register_sidebar_widget(
'secondary_widget_area', //widget_id/ widget_slug
'Secondary Widget Area', //widget_name
'secondary_widget_display', //callback function
array(
'description' => 'The secondary widget on the right side'
)
);
function secondary_widget_display( $args ) {
echo "show the secondary widget";
}
} // end theme_widgets_init
add_action( 'init', 'theme_widgets_init' );
?>
PROBLEM #1:
The Widgets sub menu under the Appearance menu in wp-admin is totally gone.
PROBLEM #2:
I’m struggling with new callback_function
of wp_register_sidebar_widget()
. How can I implement the old sidebar widget design with the new callback_function
. So, for Primary Widget Area, you can see, I simply copied & pasted the code from Codex to understand how the code is working. But as the Problem #1 occurred, I’m unable to test em.
Here is the code-block primarily used in the theme, is now deprecated. How can I use the new callback_function
to do the same?
<?php
// Deprecated Codes Below
register_sidebar_widget(
array (
'name' => 'Secondary Widget Area',
'id' => 'secondary_widget_area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
?>
NOTE: But the new code I pasted at the top solved one thing — all the DEBUG notices are gone. That’s a relief, alHamduLILLAH! 🙂
There are some problems in your code but you are close, some pointers to solve problems.
First, eliminate as much code as you can which still contains the problem. In your case remove the second widget, the less code you have to look at the easier it will be to find the problem.
Another piece of advice is to create unique widget id’s and prefix your functions, using “Primary” and names like
theme_widgets_init
is not a great idea.If you do not see any Widgets available under Appearance in the menu, if the menu item does not exist at all, it is because you need at least one active sidebar using
register_sidebar
.You need at least one of these :
For your actual widget’s issue, you’re nesting a function inside another function, the display callback needs to be outside the function.
For a second Widget you would do the same but of course with different function callback and id’s / names.
It’s really important to note that the above functions are pretty useless because the widget can only be used once in exactly 1 of the sidebars.
You really want to be extending the
WP_Widget
class:http://codex.wordpress.org/Function_Reference/register_widget