覆盖retrieve_password();在wordpress中使用自定义功能

时间:2014-02-20 13:29:59

标签: php wordpress

我想从wordpress发送一个自定义密码回复电子邮件,我可以在其中设置样式,使其看起来不那么难看。

我看到wordpress使用一个函数来执行此操作,但我不知道如何结束

function retrieve_password($user_email) {
        global $wpdb, $current_site;

        $errors = new WP_Error();

        // redefining user_login ensures we return the right case in the email
        $user_login = $user_email;

        do_action('retreive_password', $user_login);  // Misspelled and deprecated
        do_action('retrieve_password', $user_login);



        $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
        if ( empty($key) ) {
                // Generate something random for a key...
                $key = wp_generate_password(20, false);
                do_action('retrieve_password_key', $user_login, $key);
                // Now insert the new md5 key into the db
                $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
        }
        $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
        $message .= network_site_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 );

        $title = apply_filters('retrieve_password_title', $title);
        $message = apply_filters('retrieve_password_message', $message, $key);

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

        return true;
}

是否有人知道如何运行此功能而不是wp_login.php中找到的功能

由于

1 个答案:

答案 0 :(得分:1)

只需覆盖内容即可。你会看到两条特别感兴趣的线。

$title = apply_filters('retrieve_password_title', $title);
$message = apply_filters('retrieve_password_message', $message, $key);

这允许您替换已生成的标题和消息的内容。这是一个例子。

function wpse_custom_retrieve_password_message( $message, $key ) {
    return "Your custom email content";
}
add_filter( 'retrieve_password_message', 'wpse_custom_retrieve_password_message', 10, 2 );