wp_new_user_notification не работает

Обновить

April 2019

Просмотры

2.8k раз

1

Я пытаюсь настроить по умолчанию WordPress уведомления пользователя с помощью wp_new_user_notificationи я включил это в папке мю-плагин , так что получает приоритет.

<?php 
// Redefine user notification function
if ( !function_exists('wp_new_user_notification') ) {
    function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
        $user = new WP_User($user_id);

        $user_login = stripslashes($user->user_login);
        $user_email = stripslashes($user->user_email);

        $message  = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "rnrn";
        $message .= sprintf(__('Username: %s'), $user_login) . "rnrn";
        $message .= sprintf(__('E-mail: %s'), $user_email) . "rn";

        @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);

        if ( empty($plaintext_pass) )
            return;

        $message  = __('Hi there,') . "rnrn";
        $message .= sprintf(__("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "rnrn";
        $message .= wp_login_url() . "rn";
        $message .= sprintf(__('Username: %s'), $user_login) . "rn";
        $message .= sprintf(__('Password: %s'), $plaintext_pass) . "rnrn";
        $message .= sprintf(__('If you have any problems, please contact me at %s.'), get_option('admin_email')) . "rnrn";
        $message .= __('Adios!');

        wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);

    }
}
?>

Тем не менее, когда я создаю тестовый пользователь с моим адресом электронной почты он посылает мне этот шаблон

Username: test

To set your password, visit the following address:
example.com

Я хочу, чтобы иметь возможность отправить пользователю свой пароль и изменить ссылку для входа. Может кто-нибудь помочь, пожалуйста?

2 ответы

1

thanks for this solution, it works, but there is a little error inside. This function returns the md5-encoded password, not the password as plain-text for the new user. I think the best way to solve it would be to set a new password (wp_set_password) within the function:

function my_wp_new_user_notification_email($wp_new_user_notification_email,$user,$blogname) {

    $newpass = wp_generate_password( 10, true, false );
    wp_set_password($newpass,$user->ID);
    $message = sprintf(__( "Welcome to %s! Here's how to log in:" ), $blogname ) . "\r\n";
    $message .= wp_login_url() . "\r\n";
    $message .= sprintf(__( 'Username: %s' ), $user->user_login ) . "\r\n";
    $message .= sprintf(__( 'Password: %s' ), $newpass) . "\r\n\r\n";
    $message .= sprintf(__( 'If you have any problems, please contact me at %s.'), get_option( 'admin_email' ) ) . "\r\n";
    $message .= __('Adios companieros!');
    $wp_new_user_notification_email['message'] = $message;
    return $wp_new_user_notification_email;
}
2

It looks like with WP 4.9 the wp_new_user_notification() function has been altered and we have a new wp_new_user_notification_email() filter to work with. I have following code working except for the password. A value is passed but when I try to log in, the password is denied.

add_filter( 'wp_new_user_notification_email' , 'edit_user_notification_email', 10, 3 );

function edit_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {

    $message = sprintf(__( "Welcome to %s! Here's how to log in:" ), $blogname ) . "\r\n";
    $message .= wp_login_url() . "\r\n";
    $message .= sprintf(__( 'Username: %s' ), $user->user_login ) . "\r\n";
    $message .= sprintf(__( 'Password: %s' ), $user->user_pass ) . "\r\n\r\n";
    $message .= sprintf(__( 'If you have any problems, please contact me at %s.'), get_option( 'admin_email' ) ) . "\r\n";
    $message .= __('Adios!');

    $wp_new_user_notification_email['message'] = $message;

    return $wp_new_user_notification_email;

}

https://developer.wordpress.org/reference/functions/wp_new_user_notification/