首页 文章

编辑WordPress激活电子邮件

提问于
浏览
0

是否可以修改在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

据我所知,问题是所使用的过滤器很可能是我的网站没有的MultiSite设置 . 它只是一个标准的WordPress网站 .

干杯

1 回答

  • 1

    你正在寻找的功能是wp_new_user_notification它是一个pluggable function . 可插入功能是允许您覆盖Wordpress核心功能的功能 .

    您可以简单地覆盖:

    • 在mu-plugins文件夹中创建一个文件,你可以调用你想要的任何东西,但你应该给出一个有意义的名字,比如custom-new-user-notification.php

    • 复制默认函数并将其包装在if语句中,检查函数是否为最佳实践,否则会产生错误)

    • 更改消息和主题(或任何您想要更改的内容)

    例:

    <?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();
                    }
                }
    
            }
    

相关问题