How to disable edit post option after period of time?

Ok! @Kieran and @Rarst, here it is 🙂

function stoppostedition_filter( $capauser, $capask, $param){

  global $wpdb;   

  $post = get_post( $param[2] );

  if( $post->post_status == 'publish' ){

      // Disable post edit only for authore role
      if( $capauser['author'] == 1 ){

        if( ( $param[0] == "edit_post") || ( $param[0] == "delete_post" ) ) {

          // How much time have passed since post publication
          $post_time_unix = strtotime( str_replace('-', ':', $post->post_date ) );
          $current_time_unix = time();
          $diff = $current_time_unix - $post_time_unix; 
          $hours_after_publication = floor( $diff / 60 / 60 );

          // If 24 hours have passed since the publication than remove capability to edit and delete post
          if( $hours_after_publication >= 24 ){

            foreach( (array) $capask as $capasuppr) {

              if ( array_key_exists($capasuppr, $capauser) ) {

                $capauser[$capasuppr] = 0;

              }
            }
          }
        }
      }
  }
  return $capauser;
}
add_filter('user_has_cap', 'stoppostedition_filter', 100, 3 );

Leave a Comment