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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

本文标签: