覆盖默认的WP多站点通知电子邮件

时间:2011-07-23 作者:Levi

是否可以挂接并覆盖WP MS的默认通知消息?我知道正在发送的消息位于/wp admin/user new中。php

if ( is_multisite() ) {
    function admin_created_user_email( $text ) {
        /* translators: 1: Site name, 2: site URL, 3: role */
        return sprintf( __( \'Hi,
You\\\'ve been invited to join \\\'%1$s\\\' at
%2$s as a %3$s.
If you do not want to join this site please ignore
this email. This invitation will expire in a few days.

Please click the following link to activate your user account:
%%s\' ), get_bloginfo(\'name\'), site_url(), esc_html( $_REQUEST[ \'role\' ] ) );
    }
    add_filter( \'wpmu_signup_user_notification_email\', \'admin_created_user_email\' );

    function admin_created_user_subject( $text ) {
        return "[" . get_bloginfo(\'name\') . "] Your site invite";
    }
}
我相信如果我能找到正确的钩子,这样我就可以删除\\u filter(),然后添加我自己的钩子,我就能做到这一点。我一直在尝试以下功能(admin\\u created\\u user\\u email2是我的新功能):

function reset_admin_email(){
    remove_filter( \'wpmu_signup_user_notification_email\', \'admin_created_user_email\' );
    add_filter( \'wpmu_signup_user_notification_email\', \'admin_created_user_email2\', 1 );
}
我在读这一页lists the actions/hooks 我可以配合,但我不知道该用哪一个(如果其中任何一个都可以的话)

有没有人有这方面的经验来为我指明正确的方向?

谢谢Levi

6 个回复
SO网友:Aldous Wright

我可以通过添加以下内容来覆盖多站点通知电子邮件:

remove_filter(\'wpmu_signup_user_notification_email\',\'admin_created_user_email\');
add_filter(\'wpmu_signup_user_notification_email\',<function_name_here>);
add_filter(\'wpmu_signup_user_notification\',<function_name_here>);
add_filter(\'wpmu_signup_user_notification_subject\',<function_name_here>);
在底部添加三个过滤器,即电子邮件、通知和主题,可以覆盖电子邮件的内容和主题。

SO网友:Graeme

@user2647似乎走上了正确的道路,但我认为这更正确:

remove_action( \'wpmu_new_user\', \'newuser_notify_siteadmin\' );
add_action( \'wpmu_new_user\', \'my_notification\' );

function my_notification ($user_id) {
  // Make your custom notification here.
}

SO网友:Dylan Barlett

remove_filter 没有效果,因为/wp admin/user new。php运行

add_filter(\'wpmu_signup_user_notification_email\', admin_created_user_email);

每次加载时,在plugins\\u加载之后。

我通过添加一个优先级较低(数字较高)的新过滤器来实现这一点,因此它在admin_created_user_email, 具有默认优先级(10)的:

function reset_admin_email(){
    add_filter( \'wpmu_signup_user_notification_email\', \'admin_created_user_email2\', 11 );
}

SO网友:rocketman27

这种事情行得通吗?

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 = $user_login . " " . $user_email;

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

}
}

SO网友:Wyck

Dylan 答案正确,但需要详细说明。

添加一个在默认过滤器之后使用高于10的优先级触发的过滤器:

add_filter( \'wpmu_signup_user_notification_email\', \'wp_new_user_notification\', 11 );
内容的功能:

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 %s:\'), get_option(\'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\'), get_option(\'blogname\') ),
                $message
            );

            if ( empty( $plaintext_pass ) )
                return;

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

            wp_mail(
                $user_email,
                sprintf( __(\'[%s] Yourrrrrrr username and password\'), get_option(\'blogname\') ),
                $message
            );
        }
此代码是从Codex 示例core here.mu-plugins folder wp-content/mu-plugins

SO网友:badcrocodile

这些电子邮件中的大部分文本都是可翻译的,这意味着我们可以利用WP的gettext 滤器我在网上找到了以下实现翻译的方法,但找不到原始源代码。

首先定义过滤器挂钩:

add_filter(\'gettext\', [new AddAction(), \'gqa_text_strings\'], 20, 3);

然后您的翻译功能:

 /**
 * Change a few text strings related to the login/registration process
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 *
 * @param $translated_text
 * @param $text
 * @param $domain
 *
 * @return string
 */
function gqa_text_strings($translated_text, $text, $domain)
{
    switch ( $translated_text ) {
        case \'Get your own %s account in seconds\' :
            $translated_text = __(\'Request access to %s\', \'gqa\');
            break;
        case \'We send your registration email to this address. (Double-check your email address before continuing.)\' :
            $translated_text = __(\'\', \'gqa\');
            break;
        case \'But, before you can start using your new username, <strong>you must activate it</strong>.\' :
            $translated_text = __(\'An email with login instructions has been sent to the address you provided.\', \'gqa\');
            break;
        case "Check your inbox at %s and click the link given." :
            $translated_text = __(\'Check your inbox at %s.\', \'gqa\');
            break;
        case \'If you do not activate your username within two days, you will have to sign up again.\' :
            $translated_text = __(\'\', \'gqa\');
            break;
        // Subject line for new user registration email
        case \'New %1$s User: %2$s\' :
            $translated_text = __(\'Your account information\');
            break;
        // Error message when registering a new account with an already used username.
        case \'That username is currently reserved but may be available in a couple of days.\' :
            $translated_text = __(\'A user with that username already exists. Do you already have an account? Please contact site administrators or try again.\');
            break;
        // Error message when registering a new account with an already used email address.
        case \'That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.\' :
            $translated_text = __(\'That email address has already been assigned to an account. Do you already have an account? Please contact site administrators or try again\');
            break;
    }

    return $translated_text;
}
如果字符串包含在__() function. 因此,在您的情况下,您可以添加以下内容:

case \'Hi, You\\\'ve been invited to join \\\'%1$s\\\' at
%2$s as a %3$s. If you do not want to join this site please ignore
this email. This invitation will expire in a few days. Please click the following link to activate your user account: %%s\' :
    $translated_text = __(\'Whatever you want the email to say\', \'gqa\');
    break;

结束

相关推荐

wp-admin slow in multisite

使用子域多站点运行Wordpress 3.1.2(最新更新,大约一年前作为3.0安装);使用Sunrise域映射插件。前端速度很快,后端(wp admin)在网络管理站点(www.example.com/wp admin)上速度很慢,但在其中一个子域后端(foo.example.com/wp admin)上以“正常速度”运行。服务器是我们自己的,Ubuntu 10.04带有mod\\u安全性(我听说它可以减慢速度)。我能做些什么来加速后端或以某种方式运行跟踪吗?