0

In Wordpress, 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?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
Gary Woods
  • 1,011
  • 1
  • 15
  • 33
  • Or alternatively, the posts gets locked from editing after a custom amount of time. – Gary Woods Oct 17 '12 at 16:12
  • duplicate of http://wordpress.stackexchange.com/questions/69577/restrict-users-from-editing-post-based-on-the-age-of-the-post – fuxia Oct 17 '12 at 16:49

2 Answers2

1

This is the duplicate of a question on wordpress.stackexchange.com. I've copied my answer below.

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 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;
}
add_filter( 'user_has_cap', 'restrict_editing_old_posts', 10, 3 );
Community
  • 1
  • 1
Stephen M. Harris
  • 7,163
  • 3
  • 38
  • 44
-1

This should work inside a plugin

    function remove_edit() {

    // Adjust 604800 to the number of days in seconds that you want to check against. Posts will only appear if they are newer than that time.//
     if ( !current_user_can( 'manage_options' ) && (current_user_can('edit_posts') && ((current_time(timestamp) - get_the_time('U') - (get_settings('gmt_offset') * 3600) ) < 604800) )
     {
        unset( $actions['edit'] );
     }
   }
   add_filter('post_row_actions','remove_edit',10,1);

From wordpress codex http://codex.wordpress.org/Roles_and_Capabilities#edit_posts

edit_posts
  Since 2.0
  Allows access to Administration Panel options:
   Posts
   Posts > Add New
   Comments
   Comments > Awaiting Moderation
Udan
  • 5,429
  • 2
  • 28
  • 34
  • Can you please elaborate how this code works? As far as I can tell, there is no way to set which users this applies to? How do I add capabilities in this...? – Gary Woods Oct 17 '12 at 16:45
  • check tis function in the codex http://codex.wordpress.org/Function_Reference/current_user_can. – Udan Oct 17 '12 at 16:51
  • Excellent, just one final question: no matter what I set it to, Admins will always be affected. Let me explain, if I set the capability to **publish_posts**, then authors to admins will be affected by it... meaning that admins will be locked from editing posts as much as authors do. Do you understand the problem here? – Gary Woods Oct 17 '12 at 17:29
  • added condition to ignore admins – Udan Oct 17 '12 at 18:10
  • Perfect, I was not aware that was possible. Final question if you do not mind, can I execute this script through the functions.php or do I need add it in a custom plugin? – Gary Woods Oct 17 '12 at 18:21
  • I added your code to function.php and received an error. I am sure it must be a typo in your code? Here is the error: **Parse error: syntax error, unexpected '{'** – Gary Woods Oct 17 '12 at 19:00
  • 1
    Can you kindly test your code in Wordpress and confirm if it functions as proposed? – Gary Woods Oct 17 '12 at 19:08
  • @Udan I would like to know this as well. Is this working? Have you tried it Udan? – Henrik Petterson Oct 17 '12 at 21:53
  • 1
    This code would **only hide the LINK to edit** in the Dashboard post list. In other words, users with permission could still edit a post if they get ahold of the edit link. [See this answer](http://stackoverflow.com/questions/12938514/restrict-users-from-editing-post-based-on-the-age-of-the-post/12978067#12978067) to prevent all attempts to edit as well as hide links. – Stephen M. Harris Oct 30 '12 at 22:14