1

With this StackOverflow Q&A I can't make the unregister_sidebar() work for me.

Scenario

I have developed a theme using a sidebar area, where I've used a Widget. But I don't want the 'Editor' to get access to this Sidebar Widgetized Area in Admin area. So I want to do a tweak in my theme's functions.php, where I previously declared register_sidebar() somewhere to register the sidebar.

I understood that, setting a less priority will trigger the unregister function more earlier, so I tried with:

function site_unregister_sidebar() {

    if ( is_admin() && current_user_can('editor') ) {
        unregister_sidebar( 'my_custom_widget_area' );
    }
}

add_action('widgets_init', 'site_unregister_sidebar', 1);

But nothing is happening.

  • What actually I'm doing wrong?
  • Is there any other way I can achieve this?
4
  • 1
    Why don't you just register the sidebar conditionally. I can't actually see the usefulness to register a sidebar to just deregister it again inside the same theme Commented May 12, 2014 at 18:23
  • 1
    you want to run the unregister later, not earlier.
    – Milo
    Commented May 12, 2014 at 18:25
  • 1
    +1 for @PieterGoosen and to answer your question, you have to use a lower priority than one register_sidebar runs, but lower priority means higher number.
    – gmazzap
    Commented May 12, 2014 at 18:25
  • 2
    @G-M: post that as an answer please.
    – s_ha_dum
    Commented May 12, 2014 at 18:43

1 Answer 1

3

Thanks to Milo and G.M. for pointing to the master key, as G.M. said:

you have to use a lower priority than one register_sidebar runs, but lower priority means higher number.

So the final code that is working:

function site_unregister_sidebar() {

    if ( is_admin() && current_user_can('editor') ) {
        unregister_sidebar( 'my_custom_widget_area' );
    }
}

add_action('widgets_init', 'site_unregister_sidebar', 20);

Because I set my sidebar with priority of 10 with the same hook - widgets_init.

function theme_widgets_init() {

    register_sidebar( array (
        'name' => 'Custom Widget Area',
        'id' => 'my_custom_widget_area'
    ) );
}

add_action( 'widgets_init', 'theme_widgets_init', 10 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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