dousonghs58612 2014-10-28 13:00
浏览 29
已采纳

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

I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.

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条回答 默认 最新

  • doudou6050 2014-10-28 13:09
    关注

    Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.

    Some Wordpress hooks won't work inside other (custom) functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?