admin管理员组文章数量:1122832
I have the following working code:
function get_emails_by_multiple_roles ( $atts ) {
$user_args = shortcode_atts ( array (
'role' => 'role' ,
), $atts );
$users = get_users( $user_args );
foreach ( $users as $user ) :
$role_emails[] = $user->user_email;
endforeach;
$all_role_emails = implode(', ', $role_emails);
return $all_role_emails;
}
add_shortcode('multiple_roles_emails', 'get_emails_by_multiple_roles');
Ofcourse this works as following:
[multiple_roles_emails role="administrator"]
for example, but now i need help to get it to work as the following:
[multiple_roles_emails role="administrator, author"]
Any help would be appreciated.
I have the following working code:
function get_emails_by_multiple_roles ( $atts ) {
$user_args = shortcode_atts ( array (
'role' => 'role' ,
), $atts );
$users = get_users( $user_args );
foreach ( $users as $user ) :
$role_emails[] = $user->user_email;
endforeach;
$all_role_emails = implode(', ', $role_emails);
return $all_role_emails;
}
add_shortcode('multiple_roles_emails', 'get_emails_by_multiple_roles');
Ofcourse this works as following:
[multiple_roles_emails role="administrator"]
for example, but now i need help to get it to work as the following:
[multiple_roles_emails role="administrator, author"]
Any help would be appreciated.
Share Improve this question asked Sep 2, 2024 at 18:30 Troy RashTroy Rash 132 bronze badges1 Answer
Reset to default 2If you're trying to get users with all roles (in your example, administrator
and author
), your code looks like it should work.
If you want users with any role (administrator
or author
, you'll need to split the string in the role
attribute into an array using explode()
, and use the role__in
array.
Something like this should work:
function get_emails_by_multiple_roles ( $atts ) {
$atts = shortcode_atts( array('role' => '' ), $atts );
$roles = explode( ',', $atts[ 'role' ] );
// Trim whitespace from the role names.
foreach ( $roles as $key => $role ) {
$roles[ $key ] = trim( $role );
}
$user_args = array ( 'role__in' => $roles );
$users = get_users( $user_args );
foreach ( $users as $user ) :
$role_emails[] = $user->user_email;
endforeach;
$all_role_emails = implode(', ', $role_emails);
return $all_role_emails;
}
add_shortcode('multiple_roles_emails', 'get_emails_by_multiple_roles');
本文标签:
版权声明:本文标题:plugin development - Using a shortcode to get User Emails by role as specified in the shortcode, need help to allow for multiple 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295961a1929683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论