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.
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
This is just what I”m looking for Is it possible to retract it to just one custom post type?
Regards
James
It is possible but we do not have a recommended method for adding that functionality at the moment.
Ok, many thanks.
how can i make only some post id’s protected from deleting
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
Thank you.
How can we change 30 days to for example 5 days?
Edit the 19th line here: “-30 day”
Is it possible with this function to move publised post to draft after 30 days? Can you help please?
Really nice and helpful trick