I'm trying to add a verify author badge to my blog and i have been able to use this code

function add_verification_bagdge_to_authors($current_user) {
  global $post;
  $current_user = wp_get_current_user();

  $admin_role = in_array( 'administrator', (array) $current_user->roles );
  $verifed_author = in_array( 'verified_author', (array) $current_user->roles );

  $tnt_first_name = $current_user->first_name;
  $display_name = $current_user->display_name;
  $tnt_last_name = $current_user->last_name;
  $combine_names = $tnt_first_name.' '.$tnt_last_name;


  if ( $admin_role && $current_user->ID == $post->post_author ) { 
    $verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
  } elseif ( $verifed_author && $current_user->ID == $post->post_author ) { 
    $verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
  }else {
    $verify_ico = $display_name;
  }
  return $verify_ico;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
add_filter( 'get_the_author_display_name', 'add_verification_bagdge_to_authors' );

The idea is, all Administrator are automatically Verified and users with role (Verified Author) which i have created using the add_role() function is as well verified.

But with the above code, the name updates with the badge but if i'm logged in as the admin, all posts will have the author name as the admin name with the verified badge and when logged in as the verified author, all post then has the author name as the verified author and if logged out the else statement is used and no author name will show on all post.

What i want to achieve is;

  1. if it is the admin (logged in or not logged in), all post by admin should have the author name with a verified badge/icon.

  2. if user has a verified author role (logged in or not logged in), all post by such author should have their name with the verified badge/icon.

  3. ELSE (1 & 2) it should just display the name of the user without verification badge (logged in or not).

Please help or guide me in the right direction.

Thanks

The main culprit is this line

$current_user = wp_get_current_user();

As the name "get current user" suggests, it gets you the currently logged in user. That is not what you want.

Being in the the_author filter, you can use the global $authordata variable, as the function calling the filter relies on it as well.

function add_verification_bagdge_to_authors($display_name) {
    global $authordata;

    if (!is_object($authordata))
        return $display_name;

    $icon_roles = [
        'administrator',
        'verified_author',
    ];

    $found_role = false;
    foreach ($authordata->roles as $role) {
        if (in_array(strtolower($role), $icon_roles)) {
            $found_role = true;
            break;
        }
    }

    if (!$found_role)
        return $display_name;

    $result = sprintf('%s <i title="%s" class="userfnt-accept"></i>',
        $display_name,
        __('This is a Verified Author', 'plugin_or_theme_lang_slug')
    );

    return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );

get_the_author_display_name is not a core filter, so you might need to use other code for it, depending on how this filter works and where it is from.

  • Thanks so much @kero, it worked (added this filter (get_the_author_display_name), as seen here here )! but it worked for only the archive and single blog page but on the homepage, all users are checked as verified and on category page, none of the users is verified. i dont know where to add if( is_front_page() || is_category() ) { run code } .. I will as well want it to affect the comment section display name, what hook should i use?, so that verified users who comments or reply comment should have verified badge – Kolawole Emmanuel Izzy Dec 8 at 7:48
  • For the comment display name i tried adding this filter add_filter( 'get_comment_author', 'add_verification_bagdge_to_authors' ); to the code provided by @kero but again everyone got verified including those that aren't registered on the website. Not sure what i'm doing wrong – Kolawole Emmanuel Izzy Dec 8 at 8:09
  • I think the theme i was using before has something to do it the code you provided not working fully as expected ... but after i changed the theme.. i was able to manipulate the new theme and it works perfectly with your code. Thank you so much, i really appreciate the help. Please i would like to know how to add the verify badge / icon to comment section .. when a verified user comment, the badge should appear. – Kolawole Emmanuel Izzy Dec 8 at 11:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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