限制用户根据帖子的年龄编辑帖子

时间:2012-10-17 16:04:07

标签: php wordpress restrict capability

在Wordpress中,如何限制用户(基于Capabilities)在自定义时间后编辑其发布的帖子。

例如,publish_posts(作者)的用户可以编辑他们的帖子(如果超过3天),并且用户可以moderate_comments(编辑)无法编辑超过20天的任何帖子。显然,管理员可以随时编辑。

这样的事情怎么可能?

2 个答案:

答案 0 :(得分:1)

这是a question on wordpress.stackexchange.com的副本。我在下面复制了我的答案。

  

我从Wordpress user_has_cap filter codex page中获取了示例代码并对其进行了修改。将此代码添加到主题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 );

答案 1 :(得分:-1)

这应该适用于插件

    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);

来自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