1

How can I hide at the top admin bar the "new button" which is a shortcut to publish new posts, new pages etc.

I will hide from a specific role, but being new to WordPress, I am looking for the function or the archive I must to edit to do so.

I look at wp-admin and menu.php or menu-header.php.

1

The item you want to remove is a "node". When you look at the generated HTML of the WP toolbar, the ID of the list item (

  • ) contains the node.

    So in this case, for the "Add New" toolbar item, you'll see the following in the HTML:

    <li id="wp-admin-bar-new-content" class="menupop">
    

    Everything after "wp-admin-bar-" in the ID is the name of the node. In this case, "new-content".

    There are two methods you can use to remove a node. First, you can use the admin_bar_menu action hook to remove the "new-content" node as follows:

    add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
        $wp_admin_bar->remove_node( 'new-content' );
    },999);
    

    Or you can use the wp_before_admin_bar_render action hook:

    add_action( 'wp_before_admin_bar_render', function() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_node( 'new-content' );
    },999);
    

    You'll need to add some additional logic, since you want to do this for a specific user role. I would advise you here to use a "capability" and not a "role". You'll see a lot of examples on the Internet that improperly use current_user_can() to check a role name. This should only be used to check capabilities. (See this answer.)

    You didn't indicate the role (or capability) you want to restrict the node from, but for an example, let's assume that you want the node to only show if the user has "edit_posts" capability. Then your action function will look like this:

    add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
        if ( ! current_user_can( 'edit_posts' ) ) {
            $wp_admin_bar->remove_node( 'new-content' );
        }
    },999);
    

    This removes the 'new-content' node if the user does not have 'edit_posts' capability.

  • |improve this answer|||||
    • I do create a news_editor role, based on the editor role, and i do create a custom post type called news, where i need that only this role can have access to it. – Ângelo Rigo Oct 15 '19 at 19:39
    • thank you it is working fine! – Ângelo Rigo Oct 15 '19 at 21:22

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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