WordPress comments_open for specific user role or post type


comments_open is used for both post types, posts and products, the snippet below worked and allowed only (example) role users to comment

enter code here
add_action( 'init', function(){
$u = wp_get_current_user();
if( $u->exists() && in_array( 'example', (array) $u->roles, true ) ) return; add_filter( 'comments_open', '__return_false' );} );

But it also made the product reviews appear only for (example) role users and other roles cannot review products. I have tried modifying the snippet to remove post_type ==’product’ from the rule

enter code here add_action( 'init', function() {$u = wp_get_current_user();
if( $u->exists() && in_array( 'example', (array) $u->roles, true ) OR $post->post_type == 'product' ){return;} add_filter( 'comments_open', '__return_false' );} );

But still, other roles cannot review products, only (example) role users can, how do I do this? Thank you in advance

, , Moaweyah Aladwan 4 years 2020-05-23T00:10:36-05:00 0 Answers 93 views 0

Leave an answer