User redirect to destination URL after login

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?

Related posts

Leave a Reply

3 comments

  1. 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.

  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;