0

Attempting to create a Welcome Modal for first time users on initial login to my Multisite network. I'm using code from this tutorial https://wp-mix.com/wordpress-first-user-login/ I can see the User meta updating in the database, but the modal code will not insert when wrapped in this function. Modal code works otherwise.

I suspect it's either something to do with adding the function within the function, or a priority thing. But not sure where to go from here.

/**
 * Set User meta.
 */
function fyc_register_add_meta( $user_id ) {
    add_user_meta( $user_id, '_new_user', '1' );
}
add_action( 'user_register', 'fyc_register_add_meta' );


/**
 * Loads pop-up on first login.
 */
function shapeSpace_first_user_login($user_login, $user) {

    $new_user = get_user_meta( $user->ID, '_new_user', true );

        if ( $new_user ) {

        update_user_meta( $user->ID, '_new_user', '0' );

        /**
         * Embed modal in footer
         */
        function fsc_display_modal_first_login() { ?>

            <div class="welcome-modal">
                <header>
                    <h1>Welcome!</h1>
                </header>
            </div>

            <?php
        }
        add_action( 'in_admin_footer', 'fsc_display_modal_first_login' );
    }
}
add_action( 'wp_login', 'shapeSpace_first_user_login', 10, 2 );
  • Thanks got there eventually. Final working solution below. – William Bay Apr 5 '19 at 23:20
0

The problem is that wp_login comes at the end of the wp_signon() function. But that's not the end of things for logging a user in. After that, they are redirected (so that the auth cookie that is set during logon can be read). As a result, the user is wisked away to a new page an your footer action is never fired. So as written it will never work (hope it makes sense why).

You can just use your existing logic with the in_admin_footer action. Your logic doesn't really need to determine if the user is completing a login or not because once they do log in the first time, the meta is changed.

This should resolve it:

/**
 * Set User meta.
 */
function fyc_register_add_meta( $user_id ) {
    add_user_meta( $user_id, '_new_user', '1' );
}
add_action( 'user_register', 'fyc_register_add_meta' );


/**
 * Loads pop-up on first login.
 */
function shapeSpace_first_user_login($user_login, $user) {

    $new_user = get_user_meta( $user->ID, '_new_user', true );

    if ( $new_user ) {

    update_user_meta( $user->ID, '_new_user', '0' ); ?>

        <div class="welcome-modal">
            <header>
                <h1>Welcome!</h1>
            </header>
        </div>

    <?php }
}
add_action( 'in_admin_footer', 'fsc_display_modal_first_login' );
0

Well, it took some playing and troubleshooting, but I got it working. The second function did not like the arguments being passed in, so I ended up using get_current_user_id() to update the user login status.

(And decided to enqueue a JS file to launch the modal).

function fsc_new_user( $user_login, $user ) {
    if ( $new_user = get_user_meta( $user->id, '_new_user', true ) ) {
        // They've Logged In Before, set to 0.
        update_user_meta( $user->id, '_new_user', '0' );
    } else {
        // First Login, set it to 1.
        update_user_meta( $user->id, '_new_user', 1 );
    }
}
add_action( 'wp_login', 'fsc_new_user', 10, 2 );



function fsc_display_modal_first_login() {

    if ( is_user_logged_in() ) {
        // Get current total amount of logins (should be at least 1).
        $new_user = get_user_meta( get_current_user_id(), '_new_user', true );
        // If it's 1, it's their first time logging in, display the Modal.
        if ( '1' === $new_user ) {

            wp_enqueue_script( 'modal', plugin_dir_url( __FILE__ ) . 'js/flaunt-sites-core-welcome-modal.js', array(), 20190419, true );
            update_user_meta( get_current_user_id(), '_new_user', '0' );

        }
    }
}
add_action( 'admin_footer', 'fsc_display_modal_first_login' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.