Add 'manage_options' capability to an non-admin user.
I am working on a site that uses the WP2Static plugin that creates a static html version of the site. It requires that the user has the ‘manage_options‘ capability to access it. Generally only administrators have this. I want to allow additional users without having to install the User Role Editor plugin.
If I use the add_cap() to add a capability to a user then it is stored in the database and is not easily removed (I tried using remove_cap() but it didn’t change). I had to install User Role Editor to undo the change!
On the fly
The other option is to use the ‘user_has_cap’ filter. This filter will be executed a lot so the code should be as lean as possible and exit as early a possible.
In my code it checks whether the ‘manage_options’ capability is being tested before checking for the current user ID and whether it matches the user I want to give the privilege to .
It might be possible to improve the code e.g. exclude the is_user_logged_in() and wp_get_current_user() checks, because the ‘user_has_cap’ docs say that the user ID can be in the $args parameter.