$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - Hide add new page button|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Hide add new page button

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far