我能够得到一个完全符合我要求的解决方案。对于任何可能有相同任务需要解决的人;
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'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="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' );
我能够解决这个问题的方法是创建一个名为 Verified Author 的用户角色(使用来自 Wordpress Codex 的 add_role() 函数),这将是分配给整个网站的已验证作者的角色。所有具有管理员角色的用户都会被自动验证,而用户角色必须从贡献者/作者角色切换到验证作者。
上面的代码能够完成 98% 的任务,但是当经过验证的作者/管理员 cmets 时,他们的显示名称不会显示经过验证的徽章。我能够使用下面的代码来解决这个问题(将代码与简码结合)。
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',
'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="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' );
请注意:并非所有杂志主题都已正确编码/具有硬编码的块和模块元素,因此经过验证的徽章可能不适用于它们的块/模块元素。我在整个网站设计过程中都遇到过这种情况,所以我们不得不更改主题,我对新主题唯一要做的修改就是删除添加到其块/模块代码中的 html 转义,以便可以呈现图标.即在他们的模块/块代码中,他们有类似 esc_html( the_author() ) 的内容,我删除了 esc_html 以使 the_author() 一切正常。
这并不是一个真正直接的解决方案,但这就是我能够在不使用插件的情况下解决任务的方式。只需将代码添加到您的 functions.php 文件中即可。我希望它可以帮助某人。
感谢 Kero 为我指明了正确的方向,providing the code i was able to work with and manipulate