【问题标题】:Edit the WordPress Activation Email编辑 WordPress 激活电子邮件
【发布时间】:2018-03-26 13:08:42
【问题描述】:

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

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

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

<?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 网站。

干杯

【问题讨论】:

  • 我所指的激活邮件实际上是来自一个插件,并不是 WordPress 的默认功能,所以我的问题是无关紧要的!

标签: php wordpress filter


【解决方案1】:

您要查找的函数是wp_new_user_notification,它是pluggable function。可插入函数是让您覆盖 Wordpress 核心功能的函数。

您可以覆盖,只需:

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

例子:

   <?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 ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";

                    @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) . "\r\n\r\n";
                $message .= __('To set your password, visit the following address:') . "\r\n\r\n";
                $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n";

                $message .= wp_login_url() . "\r\n";

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

                if ( $switched_locale ) {
                    restore_previous_locale();
                }
            }

        }

【讨论】:

  • 感谢您对此的回复。据我所知,我所指的激活电子邮件实际上来自插件,不是默认的 WordPress 功能,所以我的问题实际上是无关紧要的!再次感谢您的帮助。
猜你喜欢
  • 2014-02-10
  • 1970-01-01
  • 2014-06-24
  • 2017-09-17
  • 2013-01-13
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多