victorzadorozhnyy
@victorzadorozhnyy

Как отправить email для смены пароля юзеру?

не могу найти функцию, которая отправляет стандартное wp письмо со ссылкой на смену пароля для пользователя.
есть панелька на js для админа и кнопка Смена пароля пользователя
resetUserPassword(id){
        var that = this;
        jQuery.post(
            Slug_API_Settings.ajaxurl,
            {
                'action': 'my_reset_password',
                'id': id,

                'nonce' : MyAjax.nonce,
                async: true
            }
        ).done( (r)=> {
            console.log('Done');
            console.log(r);
            that.props.resetUsers()
        } 
        ).error( (r)=> {
            console.log('Error');
            console.log(r);
            that.props.resetUsers()
        });
    }

Нужна функция как для админа wp_password_change_notification но для юзера,
в коде примеры того что я пробовал
add_action( 'wp_ajax_my_reset_password', 'my_reset_password' );
function my_reset_password( ) {
    $nonce = $_POST['nonce'];
    if(wp_verify_nonce( $nonce, 'myajax-nonce' )) {
        $id = sanitize_text_field($_POST["id"]);

        $user_to_reset = get_user_by('id', "$id");

        $new_pass = wp_generate_password(20, false);
//пароль меняет но письма нет
        reset_password($user_to_reset, $new_pass);


//пробовал разные функции
        //wp_password_change_notification($user_to_reset);
        //wp_set_password( $new_pass, $user_to_reset->ID );
        //update_user_option( $user_to_reset->ID, 'default_password_nag', false, true );
        //wp_new_user_notification($user_to_reset->ID, $new_pass);

        //do_action( 'after_password_reset', $user_to_reset, $new_pass );

        //do_action( 'password_reset', $user_to_reset, $new_pass );

        //wp_set_password( $new_pass, $user_to_reset->ID );
        //reset_password($user_to_reset, $new_pass);
        
        //update_user_option( $user_to_reset->ID, 'default_password_nag', false, true );
        
 //do_action( 'after_password_reset', $user_to_reset, $new_pass );
	  //wp_password_change_notification( $user_to_reset );
       
    }
    exit();
}
  • Вопрос задан
  • 215 просмотров
Решения вопроса 1
victorzadorozhnyy
@victorzadorozhnyy Автор вопроса
add_action( 'wp_ajax_my_reset_password', 'my_reset_password' );
function my_reset_password( ) {
    $nonce = $_POST['nonce'];
    if(wp_verify_nonce( $nonce, 'myajax-nonce' )) {
        $id = sanitize_text_field($_POST["id"]);

        $user_to_reset = get_user_by('id', "$id");

        my_app_retrieve_password($user_to_reset);

    }

    exit();
}

function my_app_retrieve_password($user_data) {
    global $wpdb, $current_site;

    $errors = new WP_Error();

    do_action( 'lostpassword_post', $errors );

    if ( $errors->get_error_code() )
        return $errors;

    if ( !$user_data ) {
        $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or email.'));
        return $errors;
    }

    // Redefining user_login ensures we return the right case in the email.
    $user_login = $user_data->user_login;
    $user_email = $user_data->user_email;
    $key = get_password_reset_key( $user_data );

    if ( is_wp_error( $key ) ) {
        return $key;
    }

    $message = __('Someone has requested a password reset for the following account:') . "\r\n\r\n";
    $message .= network_home_url( '/' ) . "\r\n\r\n";
    $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
    $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
    $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";

    if ( is_multisite() )
        $blogname = $GLOBALS['current_site']->site_name;
    else
        /*
         * The blogname option is escaped with esc_html on the way into the database
         * in sanitize_option we want to reverse this for the plain text arena of emails.
         */
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $title = sprintf( __('[%s] Password Reset'), $blogname );

    /**
     * Filters the subject of the password reset email.
     *
     * @since 2.8.0
     * @since 4.4.0 Added the `$user_login` and `$user_data` parameters.
     *
     * @param string  $title      Default email title.
     * @param string  $user_login The username for the user.
     * @param WP_User $user_data  WP_User object.
     */
    $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data );

    /**
     * Filters the message body of the password reset mail.
     *
     * @since 2.8.0
     * @since 4.1.0 Added `$user_login` and `$user_data` parameters.
     *
     * @param string  $message    Default mail message.
     * @param string  $key        The activation key.
     * @param string  $user_login The username for the user.
     * @param WP_User $user_data  WP_User object.
     */
    $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data );

    if ( $message && !wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) )
        wp_die( __('The email could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function.') );

    return true;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы