在独立主题中取消注册_侧边栏()不起作用

时间:2014-05-12 作者:Mayeenul Islam

具有this StackOverflow Q&A 我做不到unregister_sidebar() 为我工作。

场景

我使用侧边栏区域开发了一个主题,在那里我使用了一个小部件。但我不想让“编辑器”访问管理区域中的这个提要栏Widgetized区域。所以我想在我的主题functions.php, 我之前声明register_sidebar() 某个地方可以注册侧栏。

我知道,设置较少的优先级会更早地触发注销功能,因此我尝试了:

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);
但什么都没有发生。

我到底做错了什么

1 个回复
最合适的回答,由SO网友:Mayeenul Islam 整理而成

感谢Milo和G.M.指向主钥匙,正如G.M.所说:

您必须使用比一个register\\u边栏运行更低的优先级,但更低的优先级意味着更高的数字

因此,正在运行的最终代码是:

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);
因为我将侧边栏的优先级设置为10same 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 );

结束

相关推荐

Problem with wp_editor

我试图添加wp_editor 但没有成功。有人能帮我在“故事”字段中添加wp\\U编辑器吗。。 function guestposts_shortcode( $atts ) { extract ( shortcode_atts (array( \'cat\' => \'1\', \'author\' => \'1\', \'thanks\' => get_bloginfo(\'home\'),&