最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress limit post that subscriber can create

programmeradmin2浏览0评论

Hi my wordpress users are able to submit posts. But I want to limit the number of posts that they can submit. Can you help me?

I already tried / and / but they dont work.

Thanks!

Hi my wordpress users are able to submit posts. But I want to limit the number of posts that they can submit. Can you help me?

I already tried https://wordpress/plugins/bainternet-posts-creation-limits/ and https://cs.wordpress/plugins/wpsite-limit-posts/ but they dont work.

Thanks!

Share Improve this question asked Jun 8, 2015 at 18:49 Vale BoccaccioVale Boccaccio 1 2
  • 2 Please read How to Ask. – kaiser Commented Jun 8, 2015 at 19:01
  • By default, subscribers can't create posts at all so, first off, how was that hacked into place? – s_ha_dum Commented Jun 8, 2015 at 19:18
Add a comment  | 

1 Answer 1

Reset to default 5

This solution uses the publish post hook. What's happening here is that when a post is published, it runs the custom post_published_limit function we create below.

The $max_posts is set statically here, you can expand it to pull the value from elsewhere, but that's beyond the scope of the question being asked. However there is plenty of literature describing how to do that.

The function then gets the count of posts of the author currently trying to publish.

If the $count exceeds the $max_posts the function will save it as a draft instead of publishing it.

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 );
发布评论

评论列表(0)

  1. 暂无评论