How to redirect to a specific page when using the login widget

8 Replies ·

  1. Hello,

    I’m using the login widget on the homepage and would like to send the user to the protected page when they login.

    If this is not a feature, could you help with changing the php in the appropriate widget file?

    Thank you,
    Lori

  2. Justin Tadlock

    I do not support changing the PHP of my plugins. You would lose those changes every time an update is released.

    However, WP has a filter on login_form_defaults, which you can set to change the default redirect URL for all login forms (specific login forms can overwrite this though).

    The following code will handle this:

    add_filter( 'login_form_defaults', 'th_login_form_defaults' );
    
    function th_login_form_defaults( $defaults ) {
    
        $defaults['redirect'] = 'http://yoursite.com/page-you-want';
    
        return $defaults;
    }
    

    Just change the URL to your preferred page.

  3. Justin Tadlock

    I do not support changing the PHP of my plugins. You would lose those changes every time an update is released.

    However, WP has a filter on login_form_defaults, which you can set to change the default redirect URL for all login forms (specific login forms can overwrite this though).

    The following code will handle this:

    add_filter( 'login_form_defaults', 'th_login_form_defaults' );
    
    function th_login_form_defaults( $defaults ) {
    
        $defaults['redirect'] = 'http://yoursite.com/page-you-want';
    
        return $defaults;
    }
    

    Just change the URL to your preferred page.

  4. Eusebio Echevarria

    Hi,

    How could you do this so that it is always the protected page that they were trying to view though, rather than just a specific page?

    Thanks

  5. Justin Tadlock

    I’m not sure what you are asking. This topic is about redirecting to a custom page after the user logs in.

    If you’re wanting to change the specific page, just change http://yoursite.com/page-you-want in the above code to the URL that you prefer.

  6. Justin Tadlock

    Or, are you asking to redirect to the current page? The WP login form automatically redirects to the current page by default and shouldn’t need changing (unless you have a theme/plugin that’s overwriting that).

    If that’s the case, you can change this line:

    $defaults['redirect'] = 'http://yoursite.com/page-you-want';
    

    To this:

    $defaults['redirect'] = is_singular() ? esc_url_raw( get_permalink( get_queried_object_id() ) ) :  'http://yoursite.com/page-you-want';
    
  7. Eusebio Echevarria

    Hi,

    Sorry, I should have been clearer.

    When a user goes to access some restricted content and is greeted with the login form, via the [members_login_form] tag, they then login, but they are then just logged into the backend of the system, rather than to the original content they were trying to access.

    What I am trying to achieve is similar to the above, just rather than one specific custom URL it is dynamic based on where they first arrived to.

    I was wondering if you have a way to ensure they go back to the original content post login?

    Thanks

  8. Eusebio Echevarria

    Okay, I will have a look as to why the normal WP-login is not going back to the current page but instead going to the backend.

    Thanks for the hints, will take a look.

  9. Justin Tadlock

    The second reply I posted above should handle that.

    $defaults['redirect'] = is_singular() ? esc_url_raw( get_permalink( get_queried_object_id() ) ) :  'http://yoursite.com/page-you-want';
    

    Because only singular posts can be blocked, you’ll still want to set the http://yoursite.com/page-you-want for other pages of the site that are not blocked.

    Or, if you only ever want this behavior on singular posts that can be blocked, you can change this line:

    $defaults['redirect'] = 'http://yoursite.com/page-you-want';
    

    To this:

    if ( is_singular() ) {
    
        $defaults['redirect'] = esc_url_raw( get_permalink( get_queried_object_id() ) );
    }