Removing add new pages form editor role

admin2025-01-07  17

Hi I have set up multisite and I am the super admin. I want to remove the capabilities of "Editors" to add new pages. I made this change to my theme function, which removed what I want it too, however it also removes the button and submenu my Admin user. I have tried to put in an if condition, but not having much luck!

//hide Page
function hide_buttons()
{
    global $current_screen;

    if($current_screen->id == 'page');
    {
        echo '<style>.add-new-h2, a.page-title-action {display: none;}</style>';

    }
    
    !current_user_can('publish_posts'))
}
add_action('admin_head','hide_buttons'); // removes the "add new" button on Pages page.


if( current_user_can('editor') ) {
    
    add_action( 'admin_menu', function () {
        remove_submenu_page( 'edit.php?post_type=page', 'post-new.php post_type=page' );  //removes the sub page "add new" in Admin side bar for editor
    }, 999);

}

Hi I have set up multisite and I am the super admin. I want to remove the capabilities of "Editors" to add new pages. I made this change to my theme function, which removed what I want it too, however it also removes the button and submenu my Admin user. I have tried to put in an if condition, but not having much luck!

//hide Page
function hide_buttons()
{
    global $current_screen;

    if($current_screen->id == 'page');
    {
        echo '<style>.add-new-h2, a.page-title-action {display: none;}</style>';

    }
    
    !current_user_can('publish_posts'))
}
add_action('admin_head','hide_buttons'); // removes the "add new" button on Pages page.


if( current_user_can('editor') ) {
    
    add_action( 'admin_menu', function () {
        remove_submenu_page( 'edit.php?post_type=page', 'post-new.php post_type=page' );  //removes the sub page "add new" in Admin side bar for editor
    }, 999);

}
Share Improve this question edited Jul 6, 2023 at 14:01 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Nov 30, 2017 at 3:57 PaulPaul 115 bronze badges 3
  • 1 editor can't add pages, but can they edit pages? – Mayeenul Islam Commented Nov 30, 2017 at 8:06
  • yes they should be able to edit exiting pages, just not add new ones. – Paul Commented Nov 30, 2017 at 8:51
  • noting that removing the page doesn't really solve the problem as no capabilities are removed, so the REST API/XMLRPC etc will still publish pages if requested, and you can open the post editor but with the post_type parameter in the URL. All these things should dissapear for users who don't have those capabilities – Tom J Nowell Commented Jul 6, 2023 at 13:54
Add a comment  | 

1 Answer 1

Reset to default 0

Ok so few things.. there's a pretty well coded plugin that handles user roles and permissions: https://wordpress.org/plugins/user-role-editor/

If you'd like to do this programatically though, you should:

    <?php
function wpcodex_set_capabilities(){
      global $wp_roles; // global class wp-includes/capabilities.php
      $role = 'editor';
      $cap = 'publish_pages';
      $wp_roles->remove_cap( $role, $cap ); 
}
add_action( 'init', 'wpcodex_set_capabilities' );
    ?>

Lmk if that does it :)

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

最新回复(0)