Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Enable post comments in WordPress based on author role (https://www.warriorforum.com/programming/1467681-enable-post-comments-wordpress-based-author-role.html)

cybercli 7th March 2023 11:29 AM

Enable post comments in WordPress based on author role
 
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

garybehl 9th March 2023 07:24 AM

RE: Enable post comments in WordPress based on author role
 
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.

CyberSEO 19th June 2023 11:38 AM

Re: Enable post comments in WordPress based on author role
 
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...


All times are GMT -6. The time now is 04:34 PM.