创建不带插件的自定义欢迎电子邮件

时间:2013-02-05 作者:Jake Lisby

有没有办法定制Wordpress注册过程中发送的欢迎和验证电子邮件的电子邮件内容和主题?我想在不使用插件或“可插入”功能的情况下进行挂钩或过滤。

如果有人能给我指出正确的方向,我将非常感激。谢谢

3 个回复
最合适的回答,由SO网友:Jake Lisby 整理而成

所以我认为答案是否定的,你不能以安全的方式来做这件事。

SO网友:david.binda

恐怕你得用pluggable functions feature - 这些函数中没有过滤器或挂钩(如下面的代码所示)。更糟糕的是,对您来说,最好使用可插入功能in a plugin.

这是因为在主题函数中定义了新的可插入函数。php要求您在函数中使用新函数的定义(以便在所有插件完全加载后立即调用),这可能不好(请参阅本文下面的注释),但另一方面,它可以工作-请参阅第一个插件下面的代码。

对于那些不反对插件的人,这里有一个重写可插入函数的方法——只需将其保存到my\\u插件中即可。php(或任何其他)到插件目录,并从管理员处激活:

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin\'s Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
if( !function_exists(\'new_user_notification\') ){
function new_user_notifiaction(){
        /**
         * Notify the blog admin of a new user, normally via email.
         *
         * @since 2.0
         *
         * @param int $user_id User ID
         * @param string $plaintext_pass Optional. The user\'s plaintext password
         */
        function wp_new_user_notification($user_id, $plaintext_pass = \'\') {
            $user = get_userdata( $user_id );

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

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

            $message  = sprintf(__(\'New user registration on your site %s:\'), $blogname) . "\\r\\n\\r\\n";
            $message .= sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n\\r\\n";
            $message .= sprintf(__(\'E-mail: %s\'), $user_email) . "\\r\\n";

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

            if ( empty($plaintext_pass) )
                return;

            $message  = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
            $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
            $message .= wp_login_url() . "\\r\\n";

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

        }
    }
}
如果你好奇的话,这里有一个函数管理的相同效果。php,在另一个函数中定义了一个新函数:

//redefine wp_new_user_notification as soon as all plugins are loaded
add_action( \'plugins_loaded\', \'new_user_notifiaction\' );

function new_user_notifiaction(){
    /**
     * Notify the blog admin of a new user, normally via email.
     *
     * @since 2.0
     *
     * @param int $user_id User ID
     * @param string $plaintext_pass Optional. The user\'s plaintext password
     */
    function wp_new_user_notification($user_id, $plaintext_pass = \'\') {
        $user = get_userdata( $user_id );

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

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

        $message  = sprintf(__(\'New user registration on your site %s:\'), $blogname) . "\\r\\n\\r\\n";
        $message .= sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n\\r\\n";
        $message .= sprintf(__(\'E-mail: %s\'), $user_email) . "\\r\\n";

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

        if ( empty($plaintext_pass) )
            return;

        $message  = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
        $message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
        $message .= wp_login_url() . "\\r\\n";

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

    }
}

SO网友:optimiertes

这似乎是一个好主意,它也适用于函数。php:

// Add filter for registration email body
add_filter(\'wp_mail\',\'handle_wp_mail\');

function handle_wp_mail($atts) {
    /*"Your username and password" is the subject of the Email WordPress send from "function wp_new_user_notification" in file "wp-includes/pluggable.php"*/

    if (isset ($atts [\'subject\']) && substr_count($atts [\'subject\'],\'Your username and password\')>0 ) {
    if (isset($atts[\'message\'])) {
       $atts[\'message\'] = \'new body\';
    }
    }
    return ($atts);
}
请注意该语言是否能够获得正确的主题字符串。(来源:http://wordpress.org/support/topic/how-to-change-registration-email-content?replies=3)

结束