Enable post comments in WordPress based on author role

2 replies
I have a custom post type "public notice" whereby subscribers and paid members can create posts of that post type. However, I want to enable the comments section on posts created by paid members with the user role of "member" and disable it on posts created by free subscribers with the role of "subscriber". Is this possible?

Thanks
#author #based #comments #enable #post #role #wordpress
  • Profile picture of the author garybehl
    By default, WordPress allows users with the "Editor" and "Administrator" roles to post comments on your website. Nevertheless, you can use a plugin like "User Role Editor" or custom code to accomplish this if you want to allow users with other roles to post comments as well.
    {{ DiscussionBoard.errors[11748362].message }}
  • Profile picture of the author CyberSEO
    Yes, this is possible with WordPress, but it will require some custom coding to get it done. You can add custom code to your theme's functions.php file, or better yet, create a simple custom plugin to handle this task. Here's a simple example of how to do this:


    function cybercli_comments_open($open, $post_id) {
    $post = get_post($post_id);
    $author = get_userdata($post->post_author);
    if ($post->post_type == 'public_notice') {
    if (in_array('subscriber', $author->roles)) {
    $open = false;
    } elseif (in_array('member', $author->roles)) {
    $open = true;
    }
    }
    return $open;
    }
    add_filter('comments_open', 'cybercli_comments_open', 10, 2);


    P.S. I've tried to post the code as PHP block, but for some reason this board strips off all the variables in this case...
    Signature
    CyberSEO Pro - the almighty content syndicator for WordPress with a wide range of cutting edge AI technologies for SEO, such as OpenAI ChatGPT-4, DeepL, WordAI, Article Forge, DALL-E, Stable Diffusion and others. Promote CyberSEO Pro and earn 20% on every sale! [ VIDEO ]
    {{ DiscussionBoard.errors[11759716].message }}

Trending Topics