• Resolved jasonm4130

    (@jasonm4130)


    Hey I am using your plugin on my site. I have some specific needs to redirect certain user roles to particular pages. I have this working with the general WordPress login but if the user logs in using their social accounts (only google setup currently). They are redirect back to the home page, not following my setup redirects.

    Please see below the code that I am using for the redirect.

    // Send new users to a special page
    function redirectOnFirstLogin( $redirect_to, $requested_redirect_to, $user )
    {
        // URL to redirect to
        $redirect_first_url = '/students-welcome';
        $redirect_stuednts_url = '/students-hub';
        // How many times to redirect the user
        $num_redirects = 1;
        // If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment
        // On a new site, you might remove this setting and the associated check
        // Alternative approach: run a script to assign the "already redirected" property to all existing users
        // Alternative approach: use a date-based check so that all registered users before a certain date are ignored
        // 172800 seconds = 48 hours
        $message_period = 172800;
    
        // If they're on the login page, don't do anything
        if( !isset( $user->user_login ) )
        {
            return $redirect_to;
        }
    
        $key_name = 'redirect_on_first_login';
        // Third parameter ensures that the result is a string
        $current_redirect_value = get_user_meta( $user->ID, $key_name, true );
        if (in_array( 'student', (array) $user->roles )){
            if( strtotime( $user->user_registered ) > ( time() - $message_period )
                && ( '' == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects)
            )
            {
                if( '' != $current_redirect_value )
                {
                    $num_redirects = intval( $current_redirect_value ) + 1;
                }
                update_user_meta( $user->ID, $key_name, $num_redirects );
                return $redirect_first_url;
            }
            else
            {
                return $redirect_stuednts_url;
            }
        }
    }
    
    add_filter( 'login_redirect', 'redirectOnFirstLogin', 10, 3 );

    It seems to me as though your plugin isn’t honoring the WordPress built in login_redirect hook. Any help in fixing this would be greatly appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Nextendweb

    (@nextendweb)

    Hi @jasonm4130,
    currently we do not use WordPress default redirect filters for login and register. I should take a look and see if we could make that work in our next version or not. I will update you with details on the next week.

    Ps.: Sorry that it takes few days. We have national holiday and we are out of office until Monday.

    Thread Starter jasonm4130

    (@jasonm4130)

    Sure no worries.

    I ended up finding a solution where I wrote a custom php page that I used your inbuilt redirect to, then used that page to redirect based on user role and the other meta data. Similar to the one above.

    It’s not idea but it works. Would be happy to provide the code for anyone else with this issue, although I am sure @nextendweb would be able to write a better solution.

    Ramona

    (@nextend_ramona)

    Hi @jasonm4130

    We released a new version where we’re respecting the login_redirect WordPress filter, so you can use it override the login redirect for your different user roles.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Login Redirect’ is closed to new replies.