ThemeIM Blog Details

How to Prevent WordPress from Deleting a Post

How to Prevent WordPress from Deleting a Post

It is a good idea to archive them instead of deleting them because you never know what information you have inserted in any post. In fact, this can be the latest trend to follow. There is no reason for your posts or blogs being automatically deleted or if you don’t want to delete any blog or post forever. You may want to archive the post rather than deleting them.

— In this article, we are going to demonstrate some methods for preventing WordPress from deleting a post. Let’s start with the first method –

Using a hook or filter

By default, there are no hooks or filters preventing WordPress from deleting a post. However, you can create a filter manually using the user_has_cap shortcode. It removes the user right from deleting any post.

This filter is very powerful for blocking not only posts but almost anything from WordPress. — In order to use this filter, you have to be concerned about three parameters (arrays) –

  • $allcaps (an array, set of true or false statements)
  • $caps (an array, requested by the running operation)
  • $args (an array, arguments related to the running operation)

Add the following code in a hook or filter –

add_filter ('user_has_cap', 'wpcs_prevent_deletion', 10, 3);
function wpcs_prevent_deletion ($allcaps, $caps, $args) {
if (isset($args[0]) && isset($args[2]) && $args[0] == 'delete_post') {
$post = get_post ($args[2]);
// Insert your logic here
if ($prevent_deletion)
$allcaps[$caps[0]] = false;
}
}
return $allcaps;
}

This array turns into (‘delete_post’, $user_id, $post_id) when a post is deleted. The $caps array contains the necessary skills required to solve the deletion. It also varies according to the type of post that needs to be deleted (for example, ‘delete_published_posts’). The $allcaps array contains the corresponding elements related to the capacity of the $caps array. We need to set the $allcaps array to false to prevent WordPress from deleting a post (for example, $allcaps[$caps[0]] = false).

Related One: Enable Automatic Plugin Updates in WordPress

Manually prevent deleting posts using the function.php file

This method requires you to add code to the WordPress file. Follow the steps accordingly to activate this method –

  1. Inside the WordPress dashboard, go to –

Appearance >> Theme Editor

Theme Editor Section
  1. At the right-side panel, you will see the functions.php file.
    Appearance
  2. Copy and paste the following code in your php file.

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');

  1. This code changes the role of author users so that they cannot delete the posts.

You can also revoke permissions, just deleting the above code won’t make any difference. You must explicitly overwrite the deleted function and replace the first code snippet with the following code:
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');

Conclusion:

Now you know how to change the author’s permissions to prevent WordPress from deleting a post and revoke access to the delete option. — To deny the author’s access to post deletion options, you can use any of the methods described above.

I suggest using the first method as it is easier and result-oriented. It is sound and simple because the second method is difficult for users who do not have much coding skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.