Polar
Polar

Reputation: 3537

Changing auth_redirect() page

I understand that the auth_redirect() checks if a user is logged in, if not it redirects them to the login page then come back to the previous page on success. I need that functionality on my website and I have that.

For example, I wanted one of the pages to access only by logged in users, if they tried to access the page then they need to login first then on success come back to that page. I have the following code on my function.php.

if ( is_page('user_only') && ! is_user_logged_in() ) {
    auth_redirect();
}

The problem is I have a custom login/registration page and instead of the default WordPress login page, I want the auth_redirect(); to use my custom login/registration page.

The auth_redirect(); is using the wp-login.php and I want to use my custom page account/index.php. Can this be done? I know about wp_redirect( url, ); but I don't think I need that since its purpose is for redirection only and not for authentication.

Upvotes: 2

Views: 1290

Answers (1)

Polar
Polar

Reputation: 3537

Edit the child theme function, it is located on:

C:\xampp\htdocs\your-website\wp-content\themes\your-theme\functions.php

Then at the bottom of the code, insert these 3 functions.

FUNCTION #1 Change the default login URL which is wp-login.php to your custom page. For example https://localhost/my-website/my-account/.

/**Function to change the default `wp-login.php` with your custom login page **/
add_filter( 'login_url', 'new_login_page', 10, 3 );
function new_login_page( $login_url, $redirect, $force_reauth ) {
    $login_page = home_url( '/my-account/' ); //use the slug of your custom login page.
    return add_query_arg( 'redirect_to', $redirect, $login_page );
}

FUNCTION #2 In my case, I wanted to redirect users into Sign in/Registration page if they wanted to access the wishlist or wanted to proceed into the checkout page, after successfully logged in they will redirect back into the previous page.

/**Function to redirect into `logged-in/Registration` page if not logged-in**/
add_action('template_redirect', 'redirect_if_not_logged_in');
function redirect_if_not_logged_in() {
    if (!is_user_logged_in() && (is_page('wishlist') || is_page('checkout'))) {
        auth_redirect(); //redirect into my custom login page
    }
}

FUNCTION #3 The last thing is to handle the redirection back to the previous page after successfully logged in.

For some reason, if you are using the default login page which is the wp-login.php and not a custom login page, the redirection works without using the below code after successfully logged-in and I'm still searching for an explanation on it since I'm just new in WordPress, I think it has something to do with the custom login page of Woocommerce. Otherwise, you can use the below code to redirect back to the previous page after successful logged-in.

//function to create the redirection url
function redirect_link($redirect){
    //extract the redirection url, in my case the url with rederiction is https://my-website/my-account/?redirect_to=https://my-website/the-slug/ then I need to get the
    //https://my-website/the-slug by using the `strstr` and `substr` function of php.
    $redirect = substr(strstr($redirect, '='), 1); 

    //decode the url back to normal using the urldecode() function, otherwise the url_to_postid() won't work and will give you a different post id.
    $redirect = urldecode($redirect);

    //get the id of page that we weanted to redirect to using url_to_postid() function of wordpress.
    $redirect_page_id = url_to_postid( $redirect );

    //get the post using the id of the page
    $post = get_post($redirect_page_id); 

    //convert the id back into its original slug
    $slug = $post->post_name;

    if(!isset($slug) || trim($slug) === ''){ //if slug is empty or if doesn't exist redirect back to shop
        return get_permalink(get_page_by_path('shop'));
    }
    //re-create the url using get_permalink() and get_page_by_path() function.
    return get_permalink(get_page_by_path($slug));
}

/**Function to redirect back to previous page after succesfful logged-in**/
add_filter( 'woocommerce_login_redirect', 'redirect_back_after_logged_in');
function redirect_back_after_logged_in($redirect) {
    return redirect_link($redirect);
}

/**Function to redirect back to previous page after succesfful registration**/
add_filter( 'woocommerce_registration_redirect', 'cs_redirect_after_registration');
function cs_redirect_after_registration( $redirect ){
    return redirect_link($redirect);
}

I'm not sure if this is the right way to do it in terms of security and bug issues, I hope someone will point out the right way if there is, I will edit this if I found something better.

Upvotes: 3

Related Questions