Using a variable outside PHP Foreach Loop

2019-07-15 03:15发布

I am trying to output a username and user email outside of a foreach loop. I am trying to send an email to all WordPress users that are within a certain User Role.

Here is my code:

// Get users and their roles
    $user_args = array(
      'role__in' => 'new_role', 
      'orderby'  => 'user_nicename',
      'order'    => 'ASC'
    );  
    $users = get_users($user_args); 

    foreach ( $users as $user ) :       
        $user_name  = $user->user_email;
        $user_email = $user->display_name;          
    endforeach;

So I can then use them in other areas of the page, ie:.

echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;

I've printed out the $users array which contains the following:

Array
(
    [0] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 46
                    [user_login] => huw
                    [user_pass] => u7CqxbuQDvApUAF6tT.
                    [user_nicename] => huw
                    [user_email] => huw@xxxx.co.uk
                    [user_url] => 
                    [user_registered] => 2017-02-06 11:13:09
                    [user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
                    [user_status] => 0
                    [display_name] => Huw Daniel Rowlands
                )

            [ID] => 46
            [caps] => Array
                (
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => sssg
                    [1] => new_role
                    [2] => site_member
                    [3] => test_role
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [filter] => 
        )

    [1] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 308
                    [user_login] => jeremy
                    [user_pass] => LLOKbkPOWQsBKUIk2qL1
                    [user_nicename] => magnus
                    [user_email] => jeremy@gmail.com
                    [user_url] => 
                    [user_registered] => 2017-05-03 19:24:42
                    [user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
                    [user_status] => 0
                    [display_name] => Jeremy
                )

            [ID] => 308
            [caps] => Array
                (
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => new_role
                    [1] => sssg
                    [2] => site_member
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [filter] => 
        )

)

1条回答
可以哭但决不认输i
2楼-- · 2019-07-15 04:06

If you want to send an email to all users with a certain role, try something like this:

// Get users and their roles
$user_args = array(
  'role__in' => 'new_role', 
  'orderby'  => 'user_nicename',
  'order'    => 'ASC'
);

$users = get_users($user_args); 

foreach ( $users as $user ) :       
    $user_email  = $user->user_email;
    $user_name = $user->display_name;
    send_email($user_name, $user_email);          
endforeach;

And then write your email function:

function send_email($name, $email) {
    // Do email sending stuff here with $name & $email
}
查看更多
登录 后发表回答