WP发送邮件通知以编程方式创建新用户(WP send mail notification to ne

2019-10-21 09:47发布

我有一个脚本通过检查在外部数据库中的电子邮件地址和密码,以创建一个新的WordPress用户。 我有一个适当的脚本来启用登录用的电子邮件地址。

该脚本添加的用户从外部数据库作为工作打算,但通知电子邮件没有。

我试过(密码采用明文存储在外部数据库 - 我知道这是不建议在所有):

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

似乎没有任何工作。 下面是我用的是完整的脚本:

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;

}

所以我在做什么错?

Answer 1:

好吧,我发现了什么是错误的。

使用$ NEW_USER_ID或$用户变量将无法正常工作。

但是,使用这样的功能会工作。 我有一些邮件settigs的问题,这就是为什么它没有工作的我第一次尝试这些选项:

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

要么

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


文章来源: WP send mail notification to new user created programmatically