How to update author display name on blog posts based on user role

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

Topic verification author Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.