【问题标题】:How can I restrict users not to create new posts - Wordpress如何限制用户不创建新帖子 - Wordpress
【发布时间】:2021-12-29 13:09:26
【问题描述】:

当有人点击Add New帖子时,会出现一条消息,例如“您不允许创建帖子”,并且用户也不允许发布或起草帖子。

我试过这段代码-:

function post_published_limit( $ID, $post ) {
    $max_posts = 5; // change this or set it as an option that you can retrieve.
    $author = $post->post_author; // Post author ID.
    $count = count_user_posts( $author, 'post'); // get author post count

    if ( $count > $max_posts ) {
        // count too high, let's set it to draft.
        $post->post_status = 'draft';
        wp_update_post( $post);
    }
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );

此代码帮助我不发布帖子,但它会创建该帖子的草稿。

【问题讨论】:

  • 你对这段代码有什么问题/疑问?
  • 我什至不想草稿。
  • 那么不要包含wp_update_post( $post);
  • $post->post_status = 'draft'; 显然是发生这种情况的地方。据我了解,publish_post 钩子在实际提交帖子时运行。当用户已经经历了编写它的麻烦时,您不想拒绝它。而是找到当用户尝试创建帖子时执行的action hook
  • @MarkusAO 正确

标签: php wordpress post hook action


【解决方案1】:

我不太明白你的意思,但我认为这可以解决问题。

function post_published_limit( $ID, $post ) {
    global $current_user; 
    get_currentuserinfo(); 

    if ( user_can( $current_user, "administrator" ) ){ 
      $max_posts = 5; 
      $author = $post->post_author; // Post author ID.
      $count = count_user_posts( $author, 'post'); 

      if ( $count > $max_posts ) {
          $post->post_status = 'draft';
          wp_update_post( $post);
      }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多