Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

In other words use The SSCCE : http://sscce.org/

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 :

 function islam_widgets_sidebar() {
    // Area 1, located at the top of the sidebar.
    register_sidebar( array(
        'name' => __( 'Primary Widget Area'),
        'id' => 'primary-widget-area',
        'description' => __( 'The primary 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>',
        ) );
      }
  add_action( 'widgets_init', 'islam_widgets_sidebar' );

For your actual widget's issue, you're nesting a function inside another function, the display callback needs to be outside the function.

// Register widgetized areas
function islam_theme_widgets() {

    // Area 1
    wp_register_sidebar_widget(
        'mayeenul_widget_area',          //widget_id/ widget_slug
        'Mayeenuls Widget Area',          //widget_name
        'islam_widget_display',       //callback function
        array(
            'description' => 'The primary widget on the right side'
        )
    );

} // end theme_widgets_init

 function islam_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";

    }

add_action( 'init', 'islam_theme_widgets' );

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

share|improve this answer
    
Something is happening out of control. I tried your code, NOT WORKING! I then deleted all other codes from the functions.php and put just the code you offered - NOT WORKING! - NO WIDGETS SUB MENU! –  Mayeenul Islam Oct 12 '13 at 5:52
    
I'm not sure what you mean by submenu, the code above shows a widget under `Appearance-->Widgets-->Available Widgets' –  Wyck Oct 12 '13 at 5:54
    
Yes, the "Widgets" under "Appearance" is gone! :( –  Mayeenul Islam Oct 12 '13 at 5:59
    
I would disable any plugins you have and revert to a default theme on 3.6.1 you should get the following: i.imgur.com/wBfo1aR.jpg (The code above is the widget in the third column second one down). –  Wyck Oct 12 '13 at 6:03
1  
Oh ya do not delete all the code in a theme's function.php, you need to have at least one sidebar active for a widget to display using register_sidebar. I will update my answer gimme 2 minutes. –  Wyck Oct 12 '13 at 6:07
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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