dongxi7609 2014-12-11 12:07 采纳率: 0%
浏览 45
已采纳

WP以编程方式向新用户发送邮件通知

I have a script to create a new wordpress user by checking the email and password in a external database. I have a script in place to enable login in with the email address.

The script to add the users from the external database works as intend, but the notification emails don't.

I've tried (the password is stored in plain text in the external db - I know it's not recommended at all):

wp_new_user_notification($new_user_id, $result->det->USR_PASSWORD);
wp_new_user_notification($user->ID, $result->det->USR_PASSWORD);
wp_new_user_notification($user->data->ID, $result->det->USR_PASSWORD);

Nothing seems to work. Here is the full script I use:

add_filter('authenticate', 'uj_auth', 20, 3);

function uj_auth( $user, $username, $password ){
 if ( is_a($user, 'WP_User') ) { return $user; } //check if existing user
 if ( empty($username) || empty($password) ) {
  $error = new WP_Error();
  if ( empty($username) )
    $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
  if ( empty($password) )
    $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
return $error;
 }

$userdata = get_user_by('login', $username);

if ( !$userdata ): //if not an existing user

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,"http://webstite.com");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);  
curl_setopt($ch, CURLOPT_POSTFIELDS,"apikey=apikey&username=$username&password=$password");

$json = curl_exec ($ch);
curl_close ($ch); 
$result = json_decode($json); //the result array from the external api

if ($result->result == 'OK'): //if user and pass match those in database result is OK
    //Verify if the username is not NULL. User user-id instead
    if(is_null($result->det->USR_USERNAME)) $result->det->USR_USERNAME = $result->det->USR_ID;
  // Setup the required user information
    $userdata = array( 
          'user_email' => $result->det->USR_EMAIL,
          'user_pass' => $result->det->USR_PASSWORD,
          'user_login' => $result->det->USR_USERNAME,
          'first_name' => $result->det->USR_PRENUME,
          'last_name' => $result->det->USR_NUME
          );
  // A new user has been created
    $new_user_id = wp_insert_user( $userdata );
  // Load the new user info
    $user = new WP_User ($new_user_id);

  // Email the user
    wp_new_user_notification($new_user_id, $result->det->USR_PASSWORD);

   else:

  return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Utilizator și/sau parolă introduse greșit sau inexistente. <a href="'.site_url('inregistrare').'" title="Inregistreaza-te">Inregistrează-te!</a>')));

   endif;

else:

$userdata = apply_filters('wp_authenticate_user', $userdata, $password);
if ( is_wp_error($userdata) )
    return $userdata;

if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
    return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
    $username, wp_lostpassword_url() ) );

$user =  new WP_User($userdata->ID);

endif;

//remove_action('authenticate', 'wp_authenticate_username_password', 20);

return $user;

}

So what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duan1979768678 2014-12-11 12:49
    关注

    Ok, I find out what was wrong.

    Using the $new_user_id or $user variable won't work.

    But using the function like this will work. I had some mail settigs problems and that is why it didn't work the first time I tried these options:

    wp_new_user_notification($user->ID, $result->det->USR_PASSWORD);
    

    or

    wp_new_user_notification($user->data->ID, $result->det->USR_PASSWORD);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵