按角色列出的WordPress注册电子邮件

时间:2016-05-02 作者:jameslcarton

我想根据注册期间的角色向新用户发送一封自定义html电子邮件。因此,我有两个角色将专门在网站上注册:摄影师和订阅者,我想要一封欢迎摄影师访问网站的电子邮件,以及订阅者注册时发送的另一封电子邮件。

我知道这是一个可插入的函数,你必须在插件中重写它-从this post 但它不会按角色处理不同的电子邮件。我不确定是否可以创建一个if语句来说明当前用户是否是角色,因为我不确定在创建suer时,当前用户是否会成为因素,如果这样做有意义的话。如果您对此有任何指示,我们将不胜感激,谢谢!

1 个回复
最合适的回答,由SO网友:N00b 整理而成

向下滚动一点,大部分是WordPress的默认代码,但您必须将所有内容添加到插件中,否则可能会破坏它!我还在下面添加了一些有用的片段。复制粘贴到同一个插件。

<?php

/*
 * Plugin Name: Emails
 * Plugin URI: http://www.your-site.com
 * Description: Different registration email for different roles
 * Author: Your name
 * Author URI: http://www.your-site.com
*/ 

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

        // Message that gets sent to admin if new user registers
        $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);

        // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn\'t sent a user notifcation.
        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 ) );



        ///////////////////////////////////////////////////////////////////
        //  Your code starts here, rest of it is WordPress default code  //
        ///////////////////////////////////////////////////////////////////

        // First role
        if( $user->roles == \'some_vip_role\' ) {

            $email_subject = \'Welcome VIP!\';

            $message = \'message content for VIP\';
        } 
        // Second role
        else if( $user->roles == \'some_other_role\' ) {

            $email_subject = \'Welcome other role!\';

            $message = \'message content for other role\';
        }

        // And so on.. 


        // Send email
        wp_mail( $user->user_email, $email_subject, $message );


        ////////////////////////////////////////////////////////////
        //  Make sure to add this link to each $message           //
        //  This is where user can set his/her first password     //
        ////////////////////////////////////////////////////////////
        $message .= \'<\' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), \'login\') . ">\\r\\n\\r\\n";

    }   
}
扮演各种角色,确保一切正常同时添加else statement 最后使用泛型$email_subject$message 以防将来添加新角色时忘记更改插件$user 可以找到对象here.
  • Don\'t forget to add a link where user has to go to set a password!

      其他一些有用的代码片段-将它们添加到主函数之前,以防万一:

      // Change "from" email
      add_filter( \'wp_mail_from\', \'new_mail_from_name\' );
      
      function new_mail_from_name() {
      
          return \'hello@you-site.com\';
      }
      
      
      // Change "from" name
      add_filter( \'wp_mail_from_name\', \'new_mail_from\' );
      
      function new_mail_from() {
      
          return \'Your site\';
      }
      
      祝您编码愉快,并告诉我您的工作结果。

  • 相关推荐

    is_email gives me error

    正如您在该链接上看到的:https://cixme.deniz-tasarim.site/video-upload/单击提交按钮后,wp表示我的电子邮件无效,但我输入了有效的电子邮件。Array ( [email_valid] => Email has no valid value ) 相关代码: if(is_email($email)) { $error[\'email_valid\'] = \"Email has no valid value\";&#