Snippet: Restrict users from editing older posts

Add following snippet to “functions.php”

Snippet

add_filter( 'user_has_cap', 'wpds_restrict_editing_old_posts', 10, 3 );
function wpds_restrict_editing_old_posts( $allcaps, $cap, $args ) {

// Bail out if we're not asking to edit a post ...
if( 'edit_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;
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>