unregister_sidebar() in stand-alone theme not working

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?

Solutions Collecting From Web of "unregister_sidebar() in stand-alone theme not working"

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 hookwidgets_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 );