admin管理员组文章数量:1130349
I'm trying to add a verify author badge to my blog and i have been able to use this code
function add_verification_bagdge_to_authors($current_user) {
global $post;
$current_user = wp_get_current_user();
$admin_role = in_array( 'administrator', (array) $current_user->roles );
$verifed_author = in_array( 'verified_author', (array) $current_user->roles );
$tnt_first_name = $current_user->first_name;
$display_name = $current_user->display_name;
$tnt_last_name = $current_user->last_name;
$combine_names = $tnt_first_name.' '.$tnt_last_name;
if ( $admin_role && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
} elseif ( $verifed_author && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
}else {
$verify_ico = $display_name;
}
return $verify_ico;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
add_filter( 'get_the_author_display_name', 'add_verification_bagdge_to_authors' );
The idea is, all Administrator are automatically Verified and users with role (Verified Author) which i have created using the add_role() function is as well verified.
But with the above code, the name updates with the badge but if i'm logged in as the admin, all posts will have the author name as the admin name with the verified badge and when logged in as the verified author, all post then has the author name as the verified author and if logged out the else statement is used and no author name will show on all post.
What i want to achieve is;
if it is the admin (logged in or not logged in), all post by admin should have the author name with a verified badge/icon.
if user has a verified author role (logged in or not logged in), all post by such author should have their name with the verified badge/icon.
ELSE (1 & 2) it should just display the name of the user without verification badge (logged in or not).
Please help or guide me in the right direction.
Thanks
I'm trying to add a verify author badge to my blog and i have been able to use this code
function add_verification_bagdge_to_authors($current_user) {
global $post;
$current_user = wp_get_current_user();
$admin_role = in_array( 'administrator', (array) $current_user->roles );
$verifed_author = in_array( 'verified_author', (array) $current_user->roles );
$tnt_first_name = $current_user->first_name;
$display_name = $current_user->display_name;
$tnt_last_name = $current_user->last_name;
$combine_names = $tnt_first_name.' '.$tnt_last_name;
if ( $admin_role && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
} elseif ( $verifed_author && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
}else {
$verify_ico = $display_name;
}
return $verify_ico;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
add_filter( 'get_the_author_display_name', 'add_verification_bagdge_to_authors' );
The idea is, all Administrator are automatically Verified and users with role (Verified Author) which i have created using the add_role() function is as well verified.
But with the above code, the name updates with the badge but if i'm logged in as the admin, all posts will have the author name as the admin name with the verified badge and when logged in as the verified author, all post then has the author name as the verified author and if logged out the else statement is used and no author name will show on all post.
What i want to achieve is;
if it is the admin (logged in or not logged in), all post by admin should have the author name with a verified badge/icon.
if user has a verified author role (logged in or not logged in), all post by such author should have their name with the verified badge/icon.
ELSE (1 & 2) it should just display the name of the user without verification badge (logged in or not).
Please help or guide me in the right direction.
Thanks
Share Improve this question edited Dec 7, 2018 at 13:02 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 7, 2018 at 11:06 Kolawole Emmanuel IzzyKolawole Emmanuel Izzy 2031 silver badge8 bronze badges1 Answer
Reset to default 1The main culprit is this line
$current_user = wp_get_current_user();
As the name "get current user" suggests, it gets you the currently logged in user. That is not what you want.
Being in the the_author filter, you can use the global $authordata variable, as the function calling the filter relies on it as well.
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = [
'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="userfnt-accept"></i>',
$display_name,
__('This is a Verified Author', 'plugin_or_theme_lang_slug')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
get_the_author_display_name is not a core filter, so you might need to use other code for it, depending on how this filter works and where it is from.
I'm trying to add a verify author badge to my blog and i have been able to use this code
function add_verification_bagdge_to_authors($current_user) {
global $post;
$current_user = wp_get_current_user();
$admin_role = in_array( 'administrator', (array) $current_user->roles );
$verifed_author = in_array( 'verified_author', (array) $current_user->roles );
$tnt_first_name = $current_user->first_name;
$display_name = $current_user->display_name;
$tnt_last_name = $current_user->last_name;
$combine_names = $tnt_first_name.' '.$tnt_last_name;
if ( $admin_role && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
} elseif ( $verifed_author && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
}else {
$verify_ico = $display_name;
}
return $verify_ico;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
add_filter( 'get_the_author_display_name', 'add_verification_bagdge_to_authors' );
The idea is, all Administrator are automatically Verified and users with role (Verified Author) which i have created using the add_role() function is as well verified.
But with the above code, the name updates with the badge but if i'm logged in as the admin, all posts will have the author name as the admin name with the verified badge and when logged in as the verified author, all post then has the author name as the verified author and if logged out the else statement is used and no author name will show on all post.
What i want to achieve is;
if it is the admin (logged in or not logged in), all post by admin should have the author name with a verified badge/icon.
if user has a verified author role (logged in or not logged in), all post by such author should have their name with the verified badge/icon.
ELSE (1 & 2) it should just display the name of the user without verification badge (logged in or not).
Please help or guide me in the right direction.
Thanks
I'm trying to add a verify author badge to my blog and i have been able to use this code
function add_verification_bagdge_to_authors($current_user) {
global $post;
$current_user = wp_get_current_user();
$admin_role = in_array( 'administrator', (array) $current_user->roles );
$verifed_author = in_array( 'verified_author', (array) $current_user->roles );
$tnt_first_name = $current_user->first_name;
$display_name = $current_user->display_name;
$tnt_last_name = $current_user->last_name;
$combine_names = $tnt_first_name.' '.$tnt_last_name;
if ( $admin_role && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
} elseif ( $verifed_author && $current_user->ID == $post->post_author ) {
$verify_ico = $combine_names .' '. '<i title="This is a Verified Author" class="userfnt-accept"></i>';
}else {
$verify_ico = $display_name;
}
return $verify_ico;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
add_filter( 'get_the_author_display_name', 'add_verification_bagdge_to_authors' );
The idea is, all Administrator are automatically Verified and users with role (Verified Author) which i have created using the add_role() function is as well verified.
But with the above code, the name updates with the badge but if i'm logged in as the admin, all posts will have the author name as the admin name with the verified badge and when logged in as the verified author, all post then has the author name as the verified author and if logged out the else statement is used and no author name will show on all post.
What i want to achieve is;
if it is the admin (logged in or not logged in), all post by admin should have the author name with a verified badge/icon.
if user has a verified author role (logged in or not logged in), all post by such author should have their name with the verified badge/icon.
ELSE (1 & 2) it should just display the name of the user without verification badge (logged in or not).
Please help or guide me in the right direction.
Thanks
Share Improve this question edited Dec 7, 2018 at 13:02 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 7, 2018 at 11:06 Kolawole Emmanuel IzzyKolawole Emmanuel Izzy 2031 silver badge8 bronze badges1 Answer
Reset to default 1The main culprit is this line
$current_user = wp_get_current_user();
As the name "get current user" suggests, it gets you the currently logged in user. That is not what you want.
Being in the the_author filter, you can use the global $authordata variable, as the function calling the filter relies on it as well.
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = [
'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="userfnt-accept"></i>',
$display_name,
__('This is a Verified Author', 'plugin_or_theme_lang_slug')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
get_the_author_display_name is not a core filter, so you might need to use other code for it, depending on how this filter works and where it is from.
本文标签: verificationHow to update author display name on blog posts based on user role
版权声明:本文标题:verification - How to update author display name on blog posts based on user role 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749120802a2318973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论