...

How to Block WordPress Post Updates and Deletion After a Set Period

160

Managing WordPress website with multiple authors can be complicated sometimes. There are plugins to manage editorial workflow, but you may come across situations where you need specific solutions for better control of your WordPress site. Recently, we helped a user find such a solution. They wanted to block WordPress post updates and deletion after a set period of time for all users (including editors) after a set period of time. For example, if a published post is 30 days or older, then it cannot be edited or deleted by editors. Only administrators can modify that post. In this article, we will show you how to block post edit, updates, and deletion after a set period of time in WordPress.

Locking posts updates and deletion for older published posts in WordPress

All you need to do is add the following code in your theme’s functions.php file or in a site-specific plugin.

    function wpbeginner_restrict_editing( $allcaps, $cap, $args ) {        // Bail out if we're not asking to edit or delete a post ...      if( 'edit_post' != $args[0] && 'delete_post' != $args[0]        // ... or user is admin        || !empty( $allcaps['manage_options'] )        // ... or user already cannot edit the post        || empty( $allcaps['edit_posts'] ) )          return $allcaps;        // Load the post data:      $post = get_post( $args[2] );        // Bail out if the post isn't published:      if( 'publish' != $post->post_status )          return $allcaps;        //if post is older than 30 days. Change it to meet your needs      if( strtotime( $post->post_date ) < strtotime( '-30 day' ) ) {          //Then disallow editing.          $allcaps[$cap[0]] = FALSE;      }      return $allcaps;  }  add_filter( 'user_has_cap', 'wpbeginner_restrict_editing', 10, 3 );    

This function checks if the user has the capability to edit or delete posts. After that it checks for the post status. If a post is published and is older than 30 days, then user’s capability to edit and delete the post is taken away. If a post is published, but it is not older than 30 days, then the users with the ability to edit posts can still edit it. Note: Administrators can edit and delete posts anytime they want.

We hope that this article helped anyone who is looking to block post edit, update and deletion in WordPress after a set period of time. Would you ever do this on your site? What use cases can you see for something like this? Let us know in the comments below.

Source:
Smhmic

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.