Wordpress auto login user after registration only from a specific page

admin2025-01-08  5

I have have a custom join page that is called /join/ and the page id=484 and it uses a plugin for the registration function by simply placing a shortcode on the page very straight forward.

I need to automatically log in the user after the register and send them to a specific page. So I have created a function which works but, it also gets triggered when i'm logged in as admin and go to add new user or use the function wp_insert_user().

Here is the code that works but how do I limit the auto login to only function when they are coming from page called 'join'.

function auto_login_new_user( $user_id ) {        
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id, false, is_ssl()) ;
        wp_redirect( '.php' );
        exit;
    }
add_action( 'user_register', 'auto_login_new_user',10,1 );

Now this code doesn't work when I tell it to execute on the page id 484 why?

function auto_login_new_user( $user_id ) {        
            if (is_page('484')){
            wp_set_current_user($user_id);
            wp_set_auth_cookie($user_id, false, is_ssl()) ;
            wp_redirect( '.php' );
            exit;
        }
}
    add_action( 'user_register', 'auto_login_new_user',10,1 ); 

I have have a custom join page that is called /join/ and the page id=484 and it uses a plugin for the registration function by simply placing a shortcode on the page very straight forward.

I need to automatically log in the user after the register and send them to a specific page. So I have created a function which works but, it also gets triggered when i'm logged in as admin and go to add new user or use the function wp_insert_user().

Here is the code that works but how do I limit the auto login to only function when they are coming from page called 'join'.

function auto_login_new_user( $user_id ) {        
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id, false, is_ssl()) ;
        wp_redirect( 'http://example.com/special.php' );
        exit;
    }
add_action( 'user_register', 'auto_login_new_user',10,1 );

Now this code doesn't work when I tell it to execute on the page id 484 why?

function auto_login_new_user( $user_id ) {        
            if (is_page('484')){
            wp_set_current_user($user_id);
            wp_set_auth_cookie($user_id, false, is_ssl()) ;
            wp_redirect( 'http://example.com/special.php' );
            exit;
        }
}
    add_action( 'user_register', 'auto_login_new_user',10,1 ); 
Share Improve this question edited Sep 14, 2016 at 6:02 Nelson101 asked Sep 14, 2016 at 4:31 Nelson101Nelson101 451 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Ues this function to redirect admin to dashboard

function admin_login_redirect( $redirect_to, $request, $user )
{
    global $user;
    if( isset( $user->roles ) && is_array( $user->roles ) ) {
        if( in_array( "administrator", $user->roles ) ) {
            return $redirect_to;
        }
    }
}
add_filter("login_redirect", "admin_login_redirect", 10, 3);

For more/brief detail read this article.

Page ID don't come in quotes, See this line of your

if (is_page('484')){

Change this line like this

//Removed quotes from Page ID
if (is_page(484)){

As an alternate this can be done with Page Slug also like this

//Slug will come in quotes
if (is_page('Type_Page_Slug_Here')){
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267354a1190.html

最新回复(0)