编辑WordPress激活电子邮件


是否可以

修改用户在WordPress网站上注册帐户时发送给用户的激活电子邮件的内容?

我希望更改电子邮件的主题和正文,以阅读更符合我网站信息的内容。

我尝试将以下内容添加到mu插件中的自定义插件中,但它似乎根本没有影响:

<?php
/*
Plugin Name: Disable Username and Password Notification
Description: Disables the Username and Password email
*/
// Start changing email body
function myprefix_change_activation_email_body ($old_body, $domain, $path, $title, $user, $user_email, $key, $meta) {
    $my_message = "Hi! This is my new email! ";
    $my_message .= "Your site {$title} is almost ready. We probably also want to include the activation key: {$key} ";
    $my_message .= "Activate your site here : http://{$domain}{$path}wp-activate.php?key=$key";
    // ... other stuff
    return $my_message;
}
add_filter('wpmu_signup_blog_notification_email', 'myprefix_change_activation_email_body', 10, 8);
// End changing email body
// Start changing email subject
function myprefix_change_activation_email_subject ($old_subject, $domain, $path, $title, $user, $user_email, $key, $meta) {
    $my_subject = "Hi! You just registered on my site!";
    return $my_subject;
}
add_filter('wpmu_signup_blog_notification_subject', 'myprefix_change_activation_email_subject', 10, 8);
// End changing email subject

据我所知,问题是正在使用的过滤器最有可能用于多站点设置,而我的站点不是。 它只是一个标准的WordPress网站。

干杯

您正在寻找的函数wp_new_user_notification它是一个可插拔的函数。可插拔函数是允许您覆盖Wordpress Core功能的功能。

您可以覆盖,只需:

  1. 在mu-plugins文件夹中创建一个文件,你可以调用任何你想要的东西,但你应该给一个有意义的名字,比如custom-new-user-notification.php
  2. 复制默认函数并将其包装在 if 语句中,检查该函数是否不存在。(这是最佳实践,否则会产生错误)
  3. 更改
  4. 邮件和主题(或您要更改的任何内容)

例:

   <?php
        /*
        Plugin Name:     Custom User Notification
        Plugin URI:      http://www.example.com
        Description:     Pligin description
        Version:         1.0
        Author:          Your Name
        Author URI:      http://www.authorurl.com
        */

        if( !function_exists( 'wp_new_user_notification' ) ){
            function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
                if ( $deprecated !== null ) {
                    _deprecated_argument( __FUNCTION__, '4.3.1' );
                }
                global $wpdb, $wp_hasher;
                $user = get_userdata( $user_id );
                // 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);
                if ( 'user' !== $notify ) {
                    $switched_locale = switch_to_locale( get_locale() );
                    $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "rnrn";
                    $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "rnrn";
                    $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "rn";
                    @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message );
                    if ( $switched_locale ) {
                        restore_previous_locale();
                    }
                }
                // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
                if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
                    return;
                }
                // Generate something random for a password reset key.
                $key = wp_generate_password( 20, false );
                /** This action is documented in wp-login.php */
                do_action( 'retrieve_password_key', $user->user_login, $key );
                // Now insert the key, hashed, into the DB.
                if ( empty( $wp_hasher ) ) {
                    require_once ABSPATH . WPINC . '/class-phpass.php';
                    $wp_hasher = new PasswordHash( 8, true );
                }
                $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
                $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
                $switched_locale = switch_to_locale( get_user_locale( $user ) );
                $message = sprintf(__('Username: %s'), $user->user_login) . "rnrn";
                $message .= __('To set your password, visit the following address:') . "rnrn";
                $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">rnrn";
                $message .= wp_login_url() . "rn";
                wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message);
                if ( $switched_locale ) {
                    restore_previous_locale();
                }
            }
        }