如何在 wordpress 博客的作者姓名前添加经过验证的徽章

How to add verified badge in front of author name across wordpress blog

美好的一天, 我一直在尝试向 WordPress 用户添加验证徽章,但没有成功。我尝试使用管理员通过检查用户是否具有管理员角色并尝试在此处输入代码来进行测试 update_user_meta (display_name),但我无法将图标添加到显示名称。

我也曾尝试在名为 verify_user(文本字段)的用户配置文件中创建一个自定义字段....我在其中输入值 "verified" 并保存...我一直正在寻找要使用的挂钩,但还没见过。我不确定在此处使用哪个 hook/filter

请问是否有任何解决方案可以将此验证图标添加到作者的显示名称中,其中当单词 "verified" 输入到用户个人资料中创建的自定义字段或任何其他可用方法时(例如,如果用户有特定的角色)。我不介意我上面写的小结构是否会改变。

谢谢

我能够得到一个完全符合我要求的解决方案。对于可能有相同任务要解决的任何人;

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() 函数),这将是在整个网站上分配给经过验证的作者的角色.所有具有管理员角色的用户都会自动验证,而用户角色必须从 contributor/Author 角色切换为验证作者。

以上代码能够完成 98% 的任务,但是当经过验证的作者/管理员发表评论时,他们的显示名称不会显示经过验证的徽章。我能够使用下面的代码来解决这个问题(将代码与短代码结合起来)。

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);

如果用户是管理员或经过验证的作者return 已验证徽章的简码

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' );

请注意:并非所有杂志主题都正确编码/具有硬编码块和模块元素,因此经过验证的徽章可能无法在其块/模块元素上使用。我在整个网站设计过程中都遇到过这种情况,所以我们不得不更改主题,我对新主题所做的唯一修改是删除添加到他们的 block/module 代码中的 html 转义符,所以可以呈现图标。也就是说,在他们的 module/block 代码中,他们有类似这样的东西 esc_html( the_author() ) 并且我删除了 esc_html 以得到 the_author() 并且一切正常。

这并不是一个直接的解决方案,但这是我在不使用插件的情况下能够解决任务的方法。只需将代码添加到您的 functions.php 文件即可。我希望它能帮助某人。

感谢 Kero 为我指明了正确的方向,providing the code i was able to work with and manipulate