2

i'm trying to create a custom role, very similar to admin but without the capabilities to edit/delete plugins. Since i don't wont to use plugin for that i've edited the functions.php file adding something like this:

// Add a custom user role

$result = add_role( 'new_role', __(
'New Role' ),
array( 
'edit_posts' => true, // Allows user to edit their own posts
'edit_pages' => true, // Allows user to edit pages
'edit_others_posts' => true, // Allows user to edit others posts not just their own
'create_posts' => true, // Allows user to create new posts
'manage_categories' => true, // Allows user to manage post categories
'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
'edit_themes' => true, // false denies this capability. User can’t edit your theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => true, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
                  );

After that i can create with my admin profile a new user and assign him the role i just created. All good untill the moment i log in with the new user when i have only access to the front page of the website.

I tried to add /wp-admin after the url but i get the message:

Sorry, you are not allowed to access this page.

I've tried to add this line in the array above

'manage_options' => true, //not sure about that

but without success, the block remains

Question: How i can, without installing plugins, make a custom user role access the control panel?

Thanks, y'all

1 Answer 1

0

With help of following code you can make a custom user role access the control panel. Following code works on hooks admin_init or init

add_action( 'admin_init', 'my_custom_function' );

function my_custom_function() {

  add_role( 'new_role', 'New Role' );
  
  $role = get_role( 'new_role' );
  
  $role->add_cap( 'edit_posts' );
  $role->add_cap( 'edit_pages' );
  $role->add_cap( 'edit_others_posts' );
  $role->add_cap( 'create_posts' );
  $role->add_cap( 'manage_categories' );
  $role->add_cap( 'publish_posts' );
  $role->add_cap( 'edit_themes' );
  $role->add_cap( 'install_plugins', false );
  $role->add_cap( 'update_plugin' );
  $role->add_cap( 'update_core', false );
 
  // add more capabilities as per you requirements
}
1
  • Isn't add_role a one-time operation that updates the database? I don't think you need to run it on every admin page load.
    – Rup
    Nov 8, 2020 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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