Restrict users from editing post based on the age of the post

The question:

How can I restrict users (based on Capabilities) from editing their published posts after a custom amount of time.

For instance, a user that can publish_posts (authors) can not edit a their post if it is older than 3 days, and a user that can moderate_comments (editors) can not edit any posts that are older than 20 days. Obviously, admins can edit anytime.

How is such thing possible?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I took the example code from the WordPress user_has_cap filter codex page and modified it. Add this code to your theme functions.php:

function restrict_editing_old_posts( $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;

$post_date = strtotime( $post->post_date );
//if post is older than 30 days ...
if( $post_date < strtotime( '-30 days' )
  // ... or if older than 4 days and user is not Editor
  || ( empty($allcaps['moderate_comments']) && $post_date < strtotime('-4 days') ) ) {
    $allcaps[$cap[0]] = FALSE;
return $allcaps;
add_filter( 'user_has_cap', 'restrict_editing_old_posts', 10, 3 );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment