Home > Software design >  Redirecting User to The Destination Page in PHP Wordpress
Redirecting User to The Destination Page in PHP Wordpress

Time:09-22

I am trying to create a guest user for visitors who don't have account on the site. Following code working fine however users are redirected to the home page instead of destination page. My goal is to redirect users to where they are heading to.

if(!is_user_logged_in()){
        $creds = array();
        $creds['user_login'] = '[email protected]';
        $creds['user_password'] = '123456';
        $creds['remember'] = true;
        $user = wp_signon( $creds, false );
        wp_redirect(home_url());
    }

CodePudding user response:

It's a very good thing that I can't experiment with the php code on your site. :D try this:

if(!is_user_logged_in()){
        global $wp;
        $this_page = add_query_arg( $wp->query_vars, home_url( $wp->request ) );
        $creds = array();
        $creds['user_login'] = '[email protected]';
        $creds['user_password'] = '123456';
        $creds['remember'] = true;
        $user = wp_signon( $creds, false );
        wp_redirect($this_page);
    }
  • Related