如何在wordpress博客的作者姓名前添加经过验证的徽章 [英] How to add verified badge in front of author name across wordpress blog

查看:35
本文介绍了如何在wordpress博客的作者姓名前添加经过验证的徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Good Day, I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.

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

I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here

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

Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.

谢谢

推荐答案

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

I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;

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

The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.

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

The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).

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

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

Shortcode to return the Verified Badge if user is admin or verified author

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()一切都奏效了.

Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.

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

Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.

感谢 Kero 为我指明了正确的方向,提供我能够使用和操作的代码

Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate

这篇关于如何在wordpress博客的作者姓名前添加经过验证的徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆