2017-05-07 81 views
2

我想在foreach循環之外輸出用戶名和用戶電子郵件。我正試圖發送電子郵件給某個用戶角色中的所有WordPress用戶。使用PHP以外的變量Foreach循環

這裏是我的代碼:

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

這樣我就可以在隨後的頁面,即其他領域使用它們:.

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

我打印出來的$用戶數組,它包含以下內容:

Array 
(
    [0] => WP_User Object 
     (
      [data] => stdClass Object 
       (
        [ID] => 46 
        [user_login] => huw 
        [user_pass] => u7CqxbuQDvApUAF6tT. 
        [user_nicename] => huw 
        [user_email] => [email protected] 
        [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] => [email protected] 
        [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] => 
     ) 

) 
+0

我很困惑,你正在嘗試做什麼。如果你想在你的foreach循環中保存'user_name'和'user_email',你只需要每次覆蓋變量直到最後一次迭代。看起來好像你只期望一個用戶,那是對的嗎? – Chris

回答

1

如果你想發送電子郵件給所有用戶提供了一定的作用,嘗試這樣的事情:

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

,然後寫電子郵件功能:

function send_email($name, $email) { 
    // Do email sending stuff here with $name & $email 
} 
+0

感謝@Chris的幫助。我修改我的代碼,現在包括這個: '的foreach($用戶爲$用戶):\t \t \t \t $ USER_NAME = $用戶> USER_EMAIL; \t \t $ user_email = $ user-> display_name; \t \t send_email($ user_name,$ user_email); \t endforeach;' 然後創建一個函數所需的電子郵件內容: '函數SEND_EMAIL($ USER_NAME,USER_EMAIL $){// 主體內容在這裏... wp_mail($ ADMIN_EMAIL,$主題, $ body,$ headers); }' 但我沒有收到任何電子郵件。 –

+0

@HuwRowlands是從哪裏來的$ admin_email'?應該是'$ user_email'而不是? (另外我假設'$ subject','$ body'和'$ headers'是在使用前定義的,對嗎? – Chris

+0

對不起。我更新了函數調用以包含其他一些變量: 'function send_email $ USER_NAME,$ USER_EMAIL,$ ADMIN_EMAIL,$ POST_ID,$ cat_list){' 這些是預先定義的。'$ admin_email'爲發送電子郵件到該網站的管理員。 \t'$頭=陣列( \t 'From:Admin <'。$ admin_email。'> \ r \ n', \t'To:Admin <'。$ admin_email。'> \ r \ n', \t'Bcc:'。$ user_name。'< '。$ user_email。'> \ r \ n' );' –