0

I use the default public WordPress comment system. But I want to make my comment reply only available to admin and author user roles. How do I achieve that?

0

You can try below code to achieve your requirements.

<?php
/*check user is logged in or not*/
if ( is_user_logged_in() )
{
    if( current_user_can('author') || current_user_can('administrator') )
    {
        /*your code for logged in user having role author or administrator*/
    }
}
else
{
    /*your code for logged out user*/
}
?>

or you can also have another option.

<?php  
/*check user is logged in or not*/
if ( is_user_logged_in() )
{
    $user = wp_get_current_user();
    $allowed_roles = array('administrator', 'author');
    if( array_intersect($allowed_roles, $user->roles ) )
    {
       /*your code for logged in user having role author or administrator*/
    }
}
else
{
    /*your code for logged out user */
}
?>
  • Where do I use it, my Template or function file? – Bikram Jul 26 '18 at 15:49
  • You can use it in anywhere. – Harsh Barach Jul 26 '18 at 15:55
  • I tried Your code at the comment_reply_link on my custom comment template function, it doesn't work. – Bikram Aug 1 '18 at 12:21
  • It should work. – Harsh Barach Aug 1 '18 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.