How to Prevent Deleting Post from Author in WordPress – Stop Mishaps
WordPress has this dope feature which allows a multi-user website has its multiple users with definite permissions and abilities. It helps a lot in managing the post and getting the job done.
Read Also: How to Create a Private Post in WordPress and Undo It Back
When the user has the ‘author’ role the user can write posts and publish them on your website. They can also delete their own post be it published or not if needed.
WordPress is important for user role management system. WordPress site is assigned a user role for all registered user and all the user role comes with the permissions.
But sometimes thing can get messy you know.
Why Limit Authors From Deleting Their Own Posts in WordPress Website
The website owner can stop all of its’ the authors from deleting their own post willingly or mistakenly so that no mishap happens and the owner can have full control over the site.
Steps To Easily Stop Authors From Deleting Their Own Posts
We have included 2 methods here for limiting the access. Learn both so that it comes handy at some point of life.
Method 1: Prevent Authors from Deleting Posts By Using Plugin
Plugins are the blessings of WordPress. They are also a reason why WordPress is that famous that it is. Plugins are fantastic.
There are available plugins for that. Recommended for all users as this method below is simple.
Step 1
Install and activate the ‘Capability Manager Enhanced’ plugin which is done by locating a plugin (in our case the previous one) in the search box of the ‘Plugin’ option found in the dashboard. Activate it.
Figure 1
Step 2
After activation, visit Users » Capabilities page. It is the place where the magic happens, where you can load any WordPress user role and change its capabilities and permissions.
Figure 2
Step 3
Start by locating the ‘Select Role to View / Edit’ box in the right column. There.
Then, choose ‘Author’ user role from the drop-down menu.
Figure 3
Click on the ‘Load’ button to load the author user role capabilities. The plugin loads the ‘Author’ user role capabilities.
Step 4
Uncheck the box of the delete and delete published options under the capabilities section.
Step 5
Then just move ahead to the bottom of the page. Click on the save changes button to store your settings. The user will not able to delete any posts on your WordPress site anymore. Happy.
Allowing Back the Permission
Once capabilities are decided unless the owners want no one can change it. Even if the plugin is deleted. It is stubborn. So what to do when you want to give back the permission. Simple. Just follow the previous method (no need to install the plugin again if you have not deleted it) and check the box of the capabilities you want your users to have.
Now, if you uninstall the plugin you can return back to default WordPress capabilities firstly you need to visit Tools » Capability Manager page and click on ‘Reset to WordPress defaults’ link. It will get you to ground zero. So, chose wisely.
Method 2: Manually Preventing Authors from Deleting Their Own Posts in WordPress
Now, this is a bit tricky as you need to add code to your WordPress files for this method. Fear not.
Here is the code. Add the following code to your theme functions.php file or a site-specific plugin.
Start of Code
function wpb_change_author_role(){
global $wp_roles;
$wp_roles->remove_cap( ‘author’, ‘delete_posts’ );
$wp_roles->remove_cap( ‘author’, ‘delete_published_posts’ );
}
add_action(‘init’, ‘wpb_change_author_role’);
End of code
Above code changes the author user role and deleting his own post capability.
Giving Deleting Post Permission back
For giving back the permission removing the code it will not make it. Another instruction has to be given that too by replacing the old code with this new one.
Code Starts
function wpb_change_author_role(){
global $wp_roles;
$wp_roles->add_cap( ‘author’, ‘delete_posts’ );
$wp_roles->add_cap( ‘author’, ‘delete_published_posts’ );
}
add_action(‘init’, ‘wpb_change_author_role’);
Code End
So, yeah the previous one is easy. Stick to that.
Now you know how to stop from an accident happening. Take care of your website and your users.
Until next time!