remove_menu_page()没有在if条件下初始化

时间:2014-10-28 13:00:36

标签: php wordpress

我正在尝试根据用户角色删除一些菜单页面,但是当我在if condition中添加该功能时,它没有做任何事情。

function contributor_posts_action() {
  if ($role == 'contributor_posts') { // contributor_posts - custom role
    // echo  'here'; for testing purposes and WORKS, so it goes under the if condition
    add_action( 'admin_menu', 'remove_menus_contrib' );
    function remove_menus_contrib(){
        remove_menu_page( 'edit-comments.php' );    
        remove_menu_page( 'tools.php' );                   
        remove_menu_page( 'edit.php?post_type=directory' );          
        remove_menu_page( 'edit.php?post_type=city' );
    } // this function doesn't get hooked

    add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
    function remove_admin_bar_items( $wp_admin_bar ) {
        $wp_admin_bar->remove_node( 'new-directory' );
        $wp_admin_bar->remove_node( 'new-city' );
    }// this one works properly. It's for removing for admin bar.
  }
}
add_action( 'admin_init', 'contributor_posts_action' );

1 个答案:

答案 0 :(得分:1)

尝试从remove_menus_contrib()功能中提取add_action( 'admin_menu', 'remove_menus_contrib' )contributor_posts_action()挂钩功能。

某些Wordpress钩子不能在其他(自定义)函数中工作。