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

时间:2018-12-06 04:04:28

标签: wordpress blogs author

早安, 我一直在尝试向WordPress用户添加验证徽章,但没有成功。我尝试使用管理员进行检查,方法是检查用户是否具有角色管理员,并尝试在update_user_meta (display_name)处输入代码,但是我无法在显示名称中添加图标。

我也尝试过在用户配置文件中创建一个名为verify_user(文本字段)的自定义字段...。在其中输入了值“ verified”并保存了...我一直在寻找要使用的钩子,但还没有看不到一个。我不确定在此使用哪个挂钩/过滤器

请采取任何解决方案,将验证图标添加到作者的显示名称,在该名称中,如果在用户个人资料中创建的自定义字段中输入了“已验证”一词或任何其他可用方式(例如,如果用户具有特定角色)。我不介意我上面写的小结构是否会改变。

谢谢

1 个答案:

答案 0 :(得分:0)

我能够找到完全符合我想要的解决方案。对于可能要完成相同任务的任何人;

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

我能够解决此问题的方法是创建一个名为“验证作者”的用户角色(使用Wordpress Codex中的add_role()函数),该角色将分配给整个网站上已验证作者的角色。所有具有管理员角色的用户都将自动进行验证,而用户角色必须从贡献者/作者角色转换为已验证作者。

上面的代码能够完成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);

如果用户是管理员或经过验证的作者,则返回验证码的简码

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

请注意:并非所有Magazine主题都经过正确编码/具有硬编码的Block和Module元素,因此,经过验证的徽章可能无法在其Block / module元素上使用。我在整个网站设计过程中都经历了这一过程,因此我们不得不更改主题,而我必须对新主题进行的唯一修改就是删除添加到其块/模块代码中的html转义符,以便可以呈现图标。也就是说,在他们的模块/块代码中,他们有类似esc_html( the_author() )的内容,而我删除了esc_html,使the_author()都可以正常工作。

这并不是一个简单的解决方案,但这就是我无需使用插件就能解决任务的方式。只需将代码添加到您的functions.php文件即可。希望对您有所帮助。

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