0
votes

Trying to add ability to 'add, edit and delete pages' in Wordpress custom user role.

I am creating a custom user role below, i.e. 'sub admin'. I am trying to grant access to all 'page' abilities; but with even identifying the below, it's not working. (no 'add page, edit current page tabs displaying).

Also maybe to note; I am attempting this from a custom child theme's /function.php file. The role is displaying in the WP dashboard after the below code (i.e. Sub Admin), however I have been unsuccessful allowing there to be access to pages.

add_role(
    'sub_admin',
    __( 'Sub Admin' ),
    array(
        'read'         => true,  
        'edit_posts'   => true,
        'publish_posts' => true,
        'edit_pages'   => true,
        'edit_others_pages' => true,
        'publish_page' => true,
        'edit_pages'=>true,
        'edit_published_pages'=>true,
        'publish_pages'=>true,
        'delete_pages'=>true,
        'delete_others_pages'=>true,
        'delete_published_pages'=>true,
    )
);
1

1 Answers

0
votes

Please, try to fix your issue by trying these approaches

Please, make sure, your role has been created by getting result from add_role

$result = add_role(/*your args*/);
if ( null !== $result ) {
  echo 'Yay! New role created!';
}
else {
      echo 'Oh... the basic_contributor role already exists.';
}

If is not ok. Please, make sure, when you are trying to edit page that you have this sub_admin role

If the issue is still, please aware the wordpress developer guide notice

After the first call to add_role(), the Role and it’s Capabilities will be stored in the database!

Sequential calls will do nothing: including altering the capabilities list, which might not be the behavior that you’re expecting.

Maybe you need to remove_role() and then add_role again. Maybe you have created role first time without some capabilities.

Also, please try to use init action for adding roles

function wporg_simple_role()
{
    remove_role('sub_admin');
    // or add_role('sub_admin');
}


add_action('init', 'wporg_simple_role');

Maybe there is no editting role, the all administrator capabilities is

activate_plugins
delete_others_pages
delete_others_posts
delete_pages
delete_posts
delete_private_pages
delete_private_posts
delete_published_pages
delete_published_posts
edit_dashboard
edit_others_pages
edit_others_posts
edit_pages
edit_posts
edit_private_pages
edit_private_posts
edit_published_pages
edit_published_posts
edit_theme_options
export
import
list_users
manage_categories
manage_links
manage_options
moderate_comments
promote_users
publish_pages
publish_posts
read_private_pages
read_private_posts
read
remove_users
switch_themes
upload_files
customize
delete_site

Maybe also admin bar depends from manage_options. You could remove manage_options capability by cheking is not admin page

function change_role() {
    global $wp_roles;

    if ( ! is_admin() ) {
        return;
    }

    // if not admin page - you could temporary remove manage capabilities from 
    // sub_admin role


}

add_action('wp', 'change_role');