如何在一段时间后禁用编辑POST选项?

时间:2011-08-24 作者:Alexey

如何在发布一天后为帖子禁用编辑帖子选项?

2 个回复
最合适的回答,由SO网友:Alexey 整理而成

好的@基兰和@Rarst,在这里:)

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

SO网友:Stephen M. Harris

很好,亚历克斯。这是一个简化版本,不允许管理员进行编辑。这个版本更像是Wordpress核心的样式,正如我从Wordpress上的示例代码中获得的一样user_has_cap filter codex page 并对其进行了修改。

function restrict_editing_published_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;

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

    // Bail out if the post isn\'t published:
    if( \'publish\' != $post->post_status )
        return $allcaps;

    // If post is older than a day ...
    if( strtotime( $post->post_date ) < strtotime( \'-1 day\' ) ) {
        // ... then disallow editing.
        $allcaps[$cap[0]] = false;
    }
    return $allcaps;
}
add_filter( \'user_has_cap\', \'restrict_editing_published_posts\', 10, 3 );

结束