posts - Hide add new page button

admin2025-06-03  2

I want to hide the add new page button from users that are not administrators. I managed to hide the submenu item from the right column like this:

$page = remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );

I tried to hide the button in the page with css proposed in this question but didn't work. How I can achieve that? Here is my -failed- attempt:

if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
            echo '<style type="text/css">
            #favorite-actions, .add-new-h2, .tablenav { display:none; }
            </style>';
        }

I want to hide the add new page button from users that are not administrators. I managed to hide the submenu item from the right column like this:

$page = remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );

I tried to hide the button in the page with css proposed in this question but didn't work. How I can achieve that? Here is my -failed- attempt:

if (isset($_GET['post_type']) && $_GET['post_type'] == 'page') {
            echo '<style type="text/css">
            #favorite-actions, .add-new-h2, .tablenav { display:none; }
            </style>';
        }
Share Improve this question edited Dec 17, 2018 at 11:32 aggtr asked Dec 17, 2018 at 9:40 aggtraggtr 331 silver badge5 bronze badges 4
  • 1 Possible duplicate of How can I remove the "Add New" button in my custom post type? – Max Yudin Commented Dec 17, 2018 at 10:22
  • @MaxYudin that solution didn't work in my code – aggtr Commented Dec 17, 2018 at 11:32
  • There are three solutions. – Max Yudin Commented Dec 17, 2018 at 13:13
  • @MaxYudin none of them are suitable for my problem. I don't want to disable the capabilities because I want to hide it only for specific users. – aggtr Commented Dec 17, 2018 at 13:28
Add a comment  | 

2 Answers 2

Reset to default 2

This can be achieved easy enough with just CSS. First we are going to add a function that will output a class to the body tag based on what role the user is. Then we will enqueue a stylesheet to show up in the admin. Then use the class in the body to target and hide the button.

Add Body Class Based on User Role - Add to functions.php

function custom_role_admin_body_class( $classes ) {
    global $current_user;
    foreach( $current_user->roles as $role )
        $classes .= ' role-' . $role;
    return trim( $classes );
}
add_filter( 'admin_body_class', 'custom_role_admin_body_class' );

Enqueue Admin Styles - Add to functions.php

function custom_admin_styles(){
    wp_enqueue_style(
        'admin_css', 
        get_stylesheet_directory_uri() . '/css/admin-styles.css', array(), filemtime( get_stylesheet_directory() . '/css/admin-styles.css') 
    );
}
add_action('admin_enqueue_scripts', 'custom_admin_styles');

Use Body Class to Hide Button for NON admin users

body.edit-php.post-type-page:not(.role-administrator) .page-title-action {
    display: none;
}

install User Role Editor this plugin that will give you the option to hide options by user roles and let me know this works or not so i can give another option to do it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748915114a314778.html

最新回复(0)