Some time ago, I added the following lines to my custom theme's functions.php file, in order to allow authors on my site to edit posts and drafts from all users:
function add_theme_caps() {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');
It worked as expected. However, I now no longer want that functionality. I have removed those lines from my functions.php file, but authors can still edit all of the posts on the site. It's been a few hours since I updated the functions file in my theme's root folder and I have done hard reloads and reloads emptying the browser cache, but there is still no change.
Any ideas on what the problem could be? Could it be that those lines of code changed some other central Wordpress file? Or am I missing something by simply removing the code?
Some time ago, I added the following lines to my custom theme's functions.php file, in order to allow authors on my site to edit posts and drafts from all users:
function add_theme_caps() {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');
It worked as expected. However, I now no longer want that functionality. I have removed those lines from my functions.php file, but authors can still edit all of the posts on the site. It's been a few hours since I updated the functions file in my theme's root folder and I have done hard reloads and reloads emptying the browser cache, but there is still no change.
Any ideas on what the problem could be? Could it be that those lines of code changed some other central Wordpress file? Or am I missing something by simply removing the code?
Share Improve this question asked Apr 26, 2017 at 5:26 JCMJCM 311 silver badge8 bronze badges2 Answers
Reset to default 1It looks like I have solved my own problem. I'm guessing that a function like that creates a permanent change, regardless of whether the function remains in the functions.php file. So, to fix this, I just added a new function reversing this adding. Here is that function:
function remove_theme_caps() {
$role = get_role( 'author' );
$role->remove_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'remove_theme_caps');
This has restored the default setting of not allowing authors to edit other users' posts. And after uploading my functions file with this code, I have since deleted this section of code and re-uploaded my functions file, and the change (back to the default setting) has remained. So, problem solved!
add_cap will write to the database, this is not an operation you want to run every single time. You should hook this in very specific situations. ( most common would be register_activation_hook )