Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Or alternatively, the posts gets locked from editing after a custom amount of time... –  Amanda Duke Oct 17 '12 at 16:21
    
duplicate of stackoverflow.com/questions/12938514/… –  toscho Oct 17 '12 at 16:49
add comment

1 Answer

up vote 2 down vote accepted

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 );
share|improve this answer
2  
You're right! I changed the name of the function after testing it. I've fixed it -- it's working now. –  smhmic Oct 19 '12 at 16:26
    
can you please elaborate on how your code functions. How is it restricted? After 4 days, will an author not be able to edit posts? And who will be limited from editing the post after 30 days? Is there a way to ensure that admins are not in the mix? Meaning that admins can always edit published posts... Just some more info would be appreciated as your approach appears excellent for this question. –  Amanda Duke Oct 20 '12 at 12:01
1  
Okay I just tested it properly and absolutely excellent! I have one final request of you if you do not mind (as you seem very capable of this), is there any way to hide/disable the trash link for posts with this restriction? So the trash link will be hidden after 30 days (4 days for none-editors...) –  Amanda Duke Oct 20 '12 at 12:11
1  
By adding 'delete_post' != $args[0] to the first conditional the function will also prevent deleting posts once they can no longer be edited. I updated the code in my answer. (By the way, the inline comments explain each part of the code.) –  smhmic Oct 30 '12 at 16:31
    
Thank you very much for updating this! You have been very helpful. –  Amanda Duke Oct 31 '12 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.