7

Scenario: if user not login, user redirect to customize login page and redirect again to destination page after login. I don't use a function or plugin. This code to restriction page:

if (!is_user_logged_in()){ wp_redirect( get_option('home') . '?redirect_to=' . esc_url($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]) );
}

successfully to user login but in login page the URL change to ?page=login and can't get URL redirect to destination page. How to make it? Anyone could help, please?

3 Answers 3

4

I did something similar recently.

1. If the user is not logged in I captured the desired/destination page's URL and added it as a query arg to the login page's URL.

2. Redirect the user to the login page.

function wpa_59205_redirect(){
    global $post;
    if ( ! is_user_logged_in() ) {
                // this will tack on the current page's url as a query arg for the login page's url
        $redirect = add_query_arg( 'redirect_to', get_permalink( $post->ID ), $url_of_your_login_page_here );
                // redirect to the login page
        wp_redirect( $redirect );
        exit();
    }
}
add_action( 'template_redirect', 'wpa_59205_redirect' );

3. Then you can filter the URL that the user is redirected to after login, using the login_redirect filter. Simply check for the presence of your previously added query var:

function wpa_59205_login_redirect( $redirect_to ){
    if( isset( $_REQUEST['redirect_to'] ) ) {
        return $_REQUEST['redirect_to'];
    } else {
        return $redirect_to;
    }
}
add_filter( 'login_redirect', 'wpa_59205_login_redirect');

I did this with a WooCommerce login page, so I was filtering their proprietary login redirect filter, but login_redirect is the WP default version of the same so I think it should work, but haven't tested it.

1
  • This worked for me! Thank you very much.
    – mcometa
    Jun 6, 2020 at 11:32
2

Your snippet should work fine.

First off, your custom login form may not respect redirect_to. wp-login.php does, but you'll have to send the redirect_to argument to the wp-login.php form submission as well. Show the code for your login form.

You need to be careful how you use wp_redirect. It works by sending headers:

<?php
/**
 * Redirects to another page.
 *
 * @since 1.5.1
 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
 *
 * @param string $location The path to redirect to
 * @param int $status Status code to use
 * @return bool False if $location is not set
 */
function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters('wp_redirect', $location, $status);
    $status = apply_filters('wp_redirect_status', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
        return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
        status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);
}

So if parts of your pages have already loaded and you're not using output buffering, it won't work. Headers already sent and all that business.

If that's the case you might be better off just showing a message with a link to the login form or show the form itself (eg. wp_login_form).

Second, you should always call exit or die after using wp_redirect this causes the execution of the PHP script (eg. WordPress) to finish, send headers and bail. Otherwise things further down the page may kill your redirection headers.

<?php
wp_redirect(site_url('wp-login.php'));
exit;

Finally, if you are going to include the host in your redirect_to URL you should include the protocol.

You can also just use $_SERVER['REQUEST_URI'].

<?php
$url = add_query_arg('redirect_to', $_SERVER['REQUEST_URI'], site_url('wp-login.php'));
wp_redirect($url);
exit;
0

Please note: wp_redirect will not be called if the page has started, so make sure to call it higher up.

<?php 

    if (!is_user_logged_in()){
        $current_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
        wp_redirect( wp_login_url( $current_url ) ); 
        exit;
    }
?> 

wp_redirect: http://codex.wordpress.org/Function_Reference/wp_redirect

wp_login_url: http://codex.wordpress.org/Function_Reference/wp_login_url

1
  • "wp_redirect will not be called if the page has started". Doesn't seem like that's true. There are no checks in wp_redirect to see if output has started, etc. Dec 16, 2012 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.