Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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

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

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

9 CommentsLeave a Reply

  1. This is just what I”m looking for Is it possible to retract it to just one custom post type?

    Regards
    James

  2. Hi, I used your solution and that works fine, but How can I also hide the options of “Edit | Quick Edit | Trash | Duplicate” and show only “View” in such posts, When I click on the Edit or delete it just refreshes the page and doesn’t show any error/warning message that “You are not allowed to edit this post”

    I would like to either have messages when user clicks on Edit or Delete or just remove the options

    Thanks

  3. Is it possible with this function to move publised post to draft after 30 days? Can you help please?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.