Skip to content
Advertisement

WordPress. Used remove_cap(), getting “Sorry, you are not allowed to access this page.” even after removing this function

In WordPress v5.5.1, I have used below function to restrict authors to publish the posts:

JavaScript

It resulted in complete removal of posts page and adding new posts at wp-admin/post-new.php.

I have removed the set_capabilities() function, still getting the error “Sorry, you are not allowed to access this page.”.

I have tried resetting the wp-capabilities, still it did not worked for me.

I have tried adding a new user with author role, the new user have the same issue as well.

How can I have the authors create a post accessing wp-admin/post-new.php again?

Advertisement

Answer

Problem

After calling $author->remove_cap($cap) the capability was removed from the author role, not only in memory but also in the database (see reference section below for implementation and links). As a result, removing the call to set_capabilities() or adding a new user to the role would not reset the role capabilities.

Solutions

Approach 1 – Using WordPress

You could do the opposite of set_capabilities() eg add_capabilities() as shown below to re-assign these privileges both in memory and database using the official WordPress API

JavaScript

From your users administration page you could then ensure the user is assigned the desired role of author.

Approach 2 – Manually accessing and updating your database

Assuming wp_ as your database prefix, you could ensure that the user is indeed assigned the role of author and if not manually assign this. This is in your wp_usermeta table, you should see a record with the user_id in question and the name wp_capabilities. The value stores a php serialized array of roles for example if the user was just an author the value would be a:1:{s:6:"author";b:1;}.

Now that you have ensured the user is indeed in the role, all role capabilities are stored in wp_options table within option_name wp_user_roles. Again, this is a long serialized php associative array that is structed as such (assume $roleName to be any role eg. ‘author’)

JavaScript

If you had a backup or another working location you could restore it. If you desire to manually modify these values, due to the need for php serialization, I would recommend writing custom code in php to achieve such as a last resort if using Solution 1 is not helpful or the WordPress methods are not available eg. working from a different client/server/location.

Additional Resources and References

See implementation from official WordPress Documenation

$author = get_role(‘author’); –> returns a WP_Role in $author

And an implementation of the remove_cap called and the add_cap I will make reference to retrieved from https://developer.wordpress.org/reference/classes/wp_roles/ is included below:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement