【问题标题】:How to add verified badge in front of author name across wordpress blog如何在 wordpress 博客的作者姓名前添加经过验证的徽章
【发布时间】:2018-12-06 04:04:27
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: wordpress blogs author


    【解决方案1】:

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

    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 Codexadd_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

    【讨论】:

    • 好答案科拉+1
    猜你喜欢
    • 2019-02-24
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多