Enable post comments in WordPress based on author role

by 2 replies
3
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
#programming #author #based #comments #enable #post #role #wordpress
  • 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.
  • 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...
    • [ 1 ] Thanks
  • [DELETED]
  • [DELETED]

Next Topics on Trending Feed

  • 3

    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