Skip to content

Instantly share code, notes, and snippets.

@permanart
Created December 14, 2020 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save permanart/05e911e6349ac9d519591cbf2311f03c to your computer and use it in GitHub Desktop.
Save permanart/05e911e6349ac9d519591cbf2311f03c to your computer and use it in GitHub Desktop.
Create Verified Author WordPress with user Role
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'editor',
'your-custom-role',
);
$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="fa fa-check-circle"></i>',
$display_name,
__('This is a Verified Author', 'text-domain-here')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if ( ! empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
} else {
$author = $comment->comment_author;
}
} else {
$author = $comment->comment_author;
}
return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 1);
function add_verification_bagdge_to_authors_sc($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'editor',
'um_verified-user'
);
$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="fa fa-check-circle"></i>', $display_name, __('This is a Verified Author', 'text-domain-here') );
return $result;
}
add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
@softune
Copy link

softune commented Aug 14, 2021

Thanks for very useful code.
Sir,
Will you little modify for me . because i iam new on wordpress,
I want to display different icon based on Role,
for Administrator = Blue Color Icon
for Author = Yellow Color icon
for Contributer = green icon.

Specially For Cooment.

Please you only modify Php Code. and i will add self Custom font awesome icon.

Sorry for my bad English.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment