Redirect to referring url after login in wordpress sub-directory install

2k views Asked by At

I've read couple of previous post here but none of them is working in my case.. Basically, my blogging site is installed in a sub-directory of main website.. Main website in plain php and sub-directory is wordpress.. I allow users to read my blogs only after logged in. So, the thing is I frequently share the blog links in facebook where lots of new users come in from the link.

Main website is installed in => example.com wordpress sub-directory in => example.com/blog

As I'm using the custom template login page (login.php), whenever the non-logged in users comes- first they are redirected to example.com/blog/login. I'm using this function to redirect to login page:

function redirect_user() {

if ( ! is_user_logged_in() && !is_page( 'login' ) ) {

$return_url = esc_url('http://www.example.com/blog/login');
wp_redirect( $return_url );
exit;

  }

}

add_action( 'template_redirect', 'redirect_user' );

It redirect fine, without problem.. Then the main task of redirecting to the referrer url, I'm using the similar code above to direct to every logged in users to the referring url irrespective or post or page.. Again in the functions.php

if(is_user_logged_in())

    wp_redirect('' . $_SERVER["REQUEST_URI"]);

I thought they would work but can't seems to understand that referring url is appending the sub-directory name... For example; the above code show result as:

example.com/blog/blog/blabla-blahblah.. You see the directory name is doubling..

Anyone's advice would be highly appreciated..

1

There are 1 answers

0
hellofromTonya On

Having your WordPress website in a subdirectory will have no impact on what you are trying to do. Why? Because WordPress knows where it's located at, as you set the home and site URLs either in your wp-config.php file like this:

define('WP_HOME','http://example.com/blog');
define('WP_SITEURL','http://example.com/blog'); 

or by setting both in the Settings > General admin page:

example from my personal blog

Therefore, all of the rewrites and URLs will be relative to these URLs.

Handling the Referer Capture

When someone comes to one of the pages on your site, you want to capture that original request and add it as a redirect_to= query arg. Then you can send them to the login page.

add_action( 'wp', 'redirect_to_login_if_unauthorized', 3 );
/**
 * Redirect the user to the login, but capture the original
 * referer and add to the query arg.
 *
 * @since 1.0.0
 *
 * @param WP $wp_environment Current WordPress environment instance (passed by reference).
 *
 * @return void
 */
function redirect_to_login_if_unauthorized( WP $wp_environment ) {
    if ( is_user_logged_in() ) {
        return;
    }

    if ( $wp_environment->request ) {
        $request = home_url( add_query_arg( array(), $wp_environment->request ) );
    } else {
        $request = home_url();
    }

    $redirect = home_url() . '/wp-login.php?redirect_to=' . $request;
    wp_redirect( $redirect );
    die();
}

How it Works

The event wp fires in wp-includes/class-wp.php. It passes the object instance of the WordPress environment setup. Here is the code from WordPress Core:

    do_action_ref_array( 'wp', array( &$this ) );

This environment object has a property that we want called request. That property has the URL request (minus the blog's home URL).

If the $wp_environment->request has a value, we'll add it to the home URL as a query arg; else, we just want the home URL. Now we have the referer.

Next, you create the redirect URL, which has the path to the login page and the redirect_to query arg.

An Example

Let's say you have a post called Why I Love WordPress and the path to that post is http://example.com/blog/why-i-love-wordpress.

The value in the $request would be:

http://example.com/blog/why-i-love-wordpress

and the redirect URL would be:

http://example.com/blog/wp-login.php?redirect_to=http://example.com/why-i-love-wordpress

Upon logging in, the user is then redirected to the original page request.

Tip - Handle Logout Too

You'll want to think about the pathing after a user logs out and then build a proper request to it too.