最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

user registration - Why is wp_new_user_notification not working?

matteradmin7PV0评论

I don't like to use a plugin so I made use of user registration custom signup page and it's not sending any emails.

At first everything was working. Then I added some functions to change the "from" and the "email" using wp_mail_from, which worked. Then I decided to personalize the email as well by adding some conditions in the wp_new_user_notification. Then it doesn't send any emails any more.

Here's the whole template code:

`

<?php require_once(ABSPATH . WPINC . '/registration.php');

    /* Check if users can register. */
    $registration = get_option( 'users_can_register' );

    /* If user registered, input info. */
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
        $user_pass = wp_generate_password();
        $userdata = array(
            'user_pass' => $user_pass,
            'user_login' => esc_attr( $_POST['user_name'] ),
            'first_name' => esc_attr( $_POST['first_name'] ),
            'last_name' => esc_attr( $_POST['last_name'] ),
            'user_email' => esc_attr( $_POST['email'] ),
        );

        function make_blog_name_from_name($name) {
                return 'RNA Administrator';
                }
                add_filter('wp_mail_from_name', 'make_blog_name_from_name');

        function from_mail($content_type){
                    return '[email protected]';
                }
                add_filter('wp_mail_from','from_mail');

        if ( !$userdata['user_login'] )
            $error = __('A username is required for registration.', 'frontendprofile');
        elseif ( username_exists($userdata['user_login']) )
            $error = __('Sorry, that username already exists!', 'frontendprofile');

        elseif ( !is_email($userdata['user_email'], true) )
            $error = __('You must enter a valid email address.', 'frontendprofile');
        elseif ( email_exists($userdata['user_email']) )
            $error = __('Sorry, that email address is already used!', 'frontendprofile');

    else{
            $new_user = wp_insert_user( $userdata );
            if ( !function_exists('wp_new_user_notification') ) {
            function wp_new_user_notification($user_id, $plaintext_pass){

                $new_user = new WP_User($user_id);

                $user_login = stripslashes($user->user_login);
                $user_email = stripslashes($user->user_email);

                // 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  = sprintf(__('New user registration on your site %s:'), $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'), $blogname), $message);

                if ( empty($plaintext_pass) )
                    return;

                $message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
                $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
                $message .= "Thank you";

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

            }   
            }

        }

    }

    // calling the header.php
    get_header();?>

    <div id="container">
        <div id="content">
                <div class="entry-content">
                </div>
            <!-- REGISTER FORM STARTS HERE -->

        <?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?>

            <p class="log-in-out alert">
            <?php printf( __('You are logged in as <a href="%1$s" title="%2$s">%2$s</a>.  You don\'t need another account.', 'frontendprofile'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'frontendprofile'); ?>"><?php _e('Logout &raquo;', 'frontendprofile'); ?></a>
            </p><!-- .log-in-out .alert -->

        <?php elseif ( $new_user ) : ?>

            <p class="alert">
            <?php
                if ( current_user_can( 'create_users' ) )
                    printf( __('A user account for %1$s has been created.', 'frontendprofile'), $_POST['user-name'] );
                else 
                    printf( __('Thank you for registering, %1$s.', 'frontendprofile'), $_POST['user-name'] );
                    printf( __('<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (It might go into your spam folder)', 'frontendprofile') );
            ?>
            </p><!-- .alert -->

        <?php else : ?>

            <?php if ( $error ) : ?>
                <p class="error">
                    <?php echo $error; ?>
                </p><!-- .error -->
            <?php endif; ?>

            <?php if ( current_user_can( 'create_users' ) && $registration ) : ?>
                <p class="alert">
                    <?php _e('Users can register themselves or you can manually create users here.', 'frontendprofile'); ?>
                </p><!-- .alert -->
            <?php elseif ( current_user_can( 'create_users' ) ) : ?>
                <p class="alert">
                    <?php _e('Users cannot currently register themselves, but you can manually create users here.', 'frontendprofile'); ?>
                </p><!-- .alert -->
            <?php endif; ?>

            <?php if ( $registration || current_user_can( 'create_users' ) ) : ?>

            <form method="post" id="adduser" class="user-forms" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">


                <strong>Name</strong>

                <p class="form-username">
                    <label for="user_name"><?php _e('Username (required)', 'frontendprofile'); ?></label>
                    <input class="text-input" name="user_name" type="text" id="user_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['user_name'], 1 ); ?>" />
                </p><!-- .form-username -->

                <p class="first_name">
                    <label for="first_name"><?php _e('First Name', 'frontendprofile'); ?></label>
                    <input class="text-input" name="first_name" type="text" id="first_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['first_name'], 1 ); ?>" />
                </p><!-- .first_name -->

                <p class="last_name">
                    <label for="last_name"><?php _e('Last Name', 'frontendprofile'); ?></label>
                    <input class="text-input" name="last_name" type="text" id="last_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['last_name'], 1 ); ?>" />
                </p><!-- .last_name -->

                <strong>Contact Info</strong>

                <p class="form-email">
                    <label for="email"><?php _e('E-mail (required)', 'frontendprofile'); ?></label>
                    <input class="text-input" name="email" type="text" id="email" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>" />
                </p><!-- .form-email -->

                <p class="form-submit">
                    <?php echo $referer; ?>
                    <input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'frontendprofile'); else _e('Register', 'frontendprofile'); ?>" />
                    <?php wp_nonce_field( 'add-user' ) ?>
                    <input name="action" type="hidden" id="action" value="adduser" />
                </p><!-- .form-submit -->

            </form><!-- #adduser -->

            <?php endif; ?>

        <?php endif; ?>

<!-- REGISTER FORM ENDS HERE -->`

I don't like to use a plugin so I made use of user registration custom signup page and it's not sending any emails.

At first everything was working. Then I added some functions to change the "from" and the "email" using wp_mail_from, which worked. Then I decided to personalize the email as well by adding some conditions in the wp_new_user_notification. Then it doesn't send any emails any more.

Here's the whole template code:

`

<?php require_once(ABSPATH . WPINC . '/registration.php');

    /* Check if users can register. */
    $registration = get_option( 'users_can_register' );

    /* If user registered, input info. */
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
        $user_pass = wp_generate_password();
        $userdata = array(
            'user_pass' => $user_pass,
            'user_login' => esc_attr( $_POST['user_name'] ),
            'first_name' => esc_attr( $_POST['first_name'] ),
            'last_name' => esc_attr( $_POST['last_name'] ),
            'user_email' => esc_attr( $_POST['email'] ),
        );

        function make_blog_name_from_name($name) {
                return 'RNA Administrator';
                }
                add_filter('wp_mail_from_name', 'make_blog_name_from_name');

        function from_mail($content_type){
                    return '[email protected]';
                }
                add_filter('wp_mail_from','from_mail');

        if ( !$userdata['user_login'] )
            $error = __('A username is required for registration.', 'frontendprofile');
        elseif ( username_exists($userdata['user_login']) )
            $error = __('Sorry, that username already exists!', 'frontendprofile');

        elseif ( !is_email($userdata['user_email'], true) )
            $error = __('You must enter a valid email address.', 'frontendprofile');
        elseif ( email_exists($userdata['user_email']) )
            $error = __('Sorry, that email address is already used!', 'frontendprofile');

    else{
            $new_user = wp_insert_user( $userdata );
            if ( !function_exists('wp_new_user_notification') ) {
            function wp_new_user_notification($user_id, $plaintext_pass){

                $new_user = new WP_User($user_id);

                $user_login = stripslashes($user->user_login);
                $user_email = stripslashes($user->user_email);

                // 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  = sprintf(__('New user registration on your site %s:'), $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'), $blogname), $message);

                if ( empty($plaintext_pass) )
                    return;

                $message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
                $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
                $message .= "Thank you";

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

            }   
            }

        }

    }

    // calling the header.php
    get_header();?>

    <div id="container">
        <div id="content">
                <div class="entry-content">
                </div>
            <!-- REGISTER FORM STARTS HERE -->

        <?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?>

            <p class="log-in-out alert">
            <?php printf( __('You are logged in as <a href="%1$s" title="%2$s">%2$s</a>.  You don\'t need another account.', 'frontendprofile'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'frontendprofile'); ?>"><?php _e('Logout &raquo;', 'frontendprofile'); ?></a>
            </p><!-- .log-in-out .alert -->

        <?php elseif ( $new_user ) : ?>

            <p class="alert">
            <?php
                if ( current_user_can( 'create_users' ) )
                    printf( __('A user account for %1$s has been created.', 'frontendprofile'), $_POST['user-name'] );
                else 
                    printf( __('Thank you for registering, %1$s.', 'frontendprofile'), $_POST['user-name'] );
                    printf( __('<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (It might go into your spam folder)', 'frontendprofile') );
            ?>
            </p><!-- .alert -->

        <?php else : ?>

            <?php if ( $error ) : ?>
                <p class="error">
                    <?php echo $error; ?>
                </p><!-- .error -->
            <?php endif; ?>

            <?php if ( current_user_can( 'create_users' ) && $registration ) : ?>
                <p class="alert">
                    <?php _e('Users can register themselves or you can manually create users here.', 'frontendprofile'); ?>
                </p><!-- .alert -->
            <?php elseif ( current_user_can( 'create_users' ) ) : ?>
                <p class="alert">
                    <?php _e('Users cannot currently register themselves, but you can manually create users here.', 'frontendprofile'); ?>
                </p><!-- .alert -->
            <?php endif; ?>

            <?php if ( $registration || current_user_can( 'create_users' ) ) : ?>

            <form method="post" id="adduser" class="user-forms" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">


                <strong>Name</strong>

                <p class="form-username">
                    <label for="user_name"><?php _e('Username (required)', 'frontendprofile'); ?></label>
                    <input class="text-input" name="user_name" type="text" id="user_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['user_name'], 1 ); ?>" />
                </p><!-- .form-username -->

                <p class="first_name">
                    <label for="first_name"><?php _e('First Name', 'frontendprofile'); ?></label>
                    <input class="text-input" name="first_name" type="text" id="first_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['first_name'], 1 ); ?>" />
                </p><!-- .first_name -->

                <p class="last_name">
                    <label for="last_name"><?php _e('Last Name', 'frontendprofile'); ?></label>
                    <input class="text-input" name="last_name" type="text" id="last_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['last_name'], 1 ); ?>" />
                </p><!-- .last_name -->

                <strong>Contact Info</strong>

                <p class="form-email">
                    <label for="email"><?php _e('E-mail (required)', 'frontendprofile'); ?></label>
                    <input class="text-input" name="email" type="text" id="email" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>" />
                </p><!-- .form-email -->

                <p class="form-submit">
                    <?php echo $referer; ?>
                    <input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'frontendprofile'); else _e('Register', 'frontendprofile'); ?>" />
                    <?php wp_nonce_field( 'add-user' ) ?>
                    <input name="action" type="hidden" id="action" value="adduser" />
                </p><!-- .form-submit -->

            </form><!-- #adduser -->

            <?php endif; ?>

        <?php endif; ?>

<!-- REGISTER FORM ENDS HERE -->`
Share Improve this question edited Apr 8, 2019 at 20:41 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Nov 18, 2011 at 3:47 imuya ayumiimuya ayumi 11 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try this code. I have removed the new declaration of wp_new_user_notification function from the code as it would consider the new function only if it is put in a plugin. That's how the priority is taken.

<?php
/* Check if users can register. */
$registration = get_option( 'users_can_register' );

/* If user registered, input info. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {
    $user_pass = wp_generate_password();
    $userdata = array(
        'user_pass' => $user_pass,
        'user_login' => esc_attr( $_POST['user_name'] ),
        'first_name' => esc_attr( $_POST['first_name'] ),
        'last_name' => esc_attr( $_POST['last_name'] ),
        'user_email' => esc_attr( $_POST['email'] ),
    );

    function make_blog_name_from_name($name) {
            return 'RNA Administrator';
            }
            add_filter('wp_mail_from_name', 'make_blog_name_from_name');

    function from_mail($content_type){
                return '[email protected]';
            }
            add_filter('wp_mail_from','from_mail');

    if ( !$userdata['user_login'] )
        $error = __('A username is required for registration.', 'frontendprofile');
    elseif ( username_exists($userdata['user_login']) )
        $error = __('Sorry, that username already exists!', 'frontendprofile');

    elseif ( !is_email($userdata['user_email']) )
        $error = __('You must enter a valid email address.', 'frontendprofile');
    elseif ( email_exists($userdata['user_email']) )
        $error = __('Sorry, that email address is already used!', 'frontendprofile');

else{
        $new_user = wp_insert_user( $userdata );

        wp_new_user_notification($new_user, $user_pass);

    }

}

// calling the header.php
get_header();?>

<div id="container">
    <div id="content">
            <div class="entry-content">
            </div>
        <!-- REGISTER FORM STARTS HERE -->

    <?php if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) : ?>

        <p class="log-in-out alert">
        <?php printf( __('You are logged in as <a href="%1$s" title="%2$s">%2$s</a>.  You don\'t need another account.', 'frontendprofile'), get_author_posts_url( $curauth->ID ), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'frontendprofile'); ?>"><?php _e('Logout &raquo;', 'frontendprofile'); ?></a>
        </p><!-- .log-in-out .alert -->

    <?php elseif ( @$new_user ) : ?>

        <p class="alert">
        <?php
            if ( current_user_can( 'create_users' ) )
                printf( __('A user account for %1$s has been created.', 'frontendprofile'), $_POST['user_name'] );
            else 
                printf( __('Thank you for registering, %1$s.', 'frontendprofile'), $_POST['user-name'] );
                printf( __('<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (It might go into your spam folder)', 'frontendprofile') );
        ?>
        </p><!-- .alert -->

    <?php else : ?>

        <?php if ( $error ) : ?>
            <p class="error">
                <?php echo $error; ?>
            </p><!-- .error -->
        <?php endif; ?>

        <?php if ( current_user_can( 'create_users' ) && $registration ) : ?>
            <p class="alert">
                <?php _e('Users can register themselves or you can manually create users here.', 'frontendprofile'); ?>
            </p><!-- .alert -->
        <?php elseif ( current_user_can( 'create_users' ) ) : ?>
            <p class="alert">
                <?php _e('Users cannot currently register themselves, but you can manually create users here.', 'frontendprofile'); ?>
            </p><!-- .alert -->
        <?php endif; ?>

        <?php if ( $registration || current_user_can( 'create_users' ) ) : ?>

        <form method="post" id="adduser" class="user-forms" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>">


            <strong>Name</strong>

            <p class="form-username">
                <label for="user_name"><?php _e('Username (required)', 'frontendprofile'); ?></label>
                <input class="text-input" name="user_name" type="text" id="user_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['user_name'], 1 ); ?>" />
            </p><!-- .form-username -->

            <p class="first_name">
                <label for="first_name"><?php _e('First Name', 'frontendprofile'); ?></label>
                <input class="text-input" name="first_name" type="text" id="first_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['first_name'], 1 ); ?>" />
            </p><!-- .first_name -->

            <p class="last_name">
                <label for="last_name"><?php _e('Last Name', 'frontendprofile'); ?></label>
                <input class="text-input" name="last_name" type="text" id="last_name" value="<?php if ( $error ) echo wp_specialchars( $_POST['last_name'], 1 ); ?>" />
            </p><!-- .last_name -->

            <strong>Contact Info</strong>

            <p class="form-email">
                <label for="email"><?php _e('E-mail (required)', 'frontendprofile'); ?></label>
                <input class="text-input" name="email" type="text" id="email" value="<?php if ( $error ) echo wp_specialchars( $_POST['email'], 1 ); ?>" />
            </p><!-- .form-email -->

            <p class="form-submit">
                <input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if ( current_user_can( 'create_users' ) ) _e('Add User', 'frontendprofile'); else _e('Register', 'frontendprofile'); ?>" />
                <?php wp_nonce_field( 'add-user' ) ?>
                <input name="action" type="hidden" id="action" value="adduser" />
            </p><!-- .form-submit -->

        </form><!-- #adduser -->

        <?php endif; ?>

    <?php endif; ?>
Post a comment

comment list (0)

  1. No comments so far