• Resolved fibbo8

    (@fibbo8)


    Hy!

    I’m creating a page in a local installation.
    I’ve inserted a page with a link to wp-login.

    I’d wished to have a redirect to that page, after a user had logged in as a subscriber.
    So in function.php I’ve insert this piece of code:

    function loginRedirect( $redirect_to, $request, $user ) {
        if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
            return get_bloginfo( 'siteurl' );
        }
        return $request;}
    add_filter( 'login_redirect', 'loginRedirect', 10, 3 );

    But with this code operating, after the login the user is redirect to the home page.
    I’ve tried to change the code, using the wp_get_referer function:

    function loginRedirect( $redirect_to, $request, $user ) {
        if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
            return get_bloginfo( 'siteurl' );
        }
        return wp_safe_redirect( wp_get_referer() );}
    add_filter( 'login_redirect', 'loginRedirect', 10, 3 );

    In this case the link in the page that point to “wp-login.php” redirect directly to the current page and the user haven’t the possibility to log-in.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi fibbo8 –

    If I’m understanding you correctly it looks like you are really close to getting this to work. You can go about this redirect many different ways (including finding a plugin that would help you set up the redirect). However, a php snippet is below that should work for you:

    /**
     * WordPress function for redirecting users on login based on user role
     */
    function my_login_redirect( $url, $request, $user ){
        if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
            if( $user->has_cap( 'administrator' ) ) {
                $url = admin_url();
            } else {
                $url = home_url('/members-only/');
            }
        }
        return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    **Please note this snippet above takes any user that is not an administrator and redirects them to the front end page /members-only/. You can change the /members-only/ link to whatever link you need.

    Thread Starter fibbo8

    (@fibbo8)

    Hi Belly!

    Thank you for your reply.
    I tried your snippet for a page and it works perfectly.
    I’ve tried to modified it, because of the structure that I’ve on the test site.
    My idea was to create some pages accessible only by logged user, but those page haven’t necessarily to be under the same path.
    For example I can have https://www.testsite.com/private-page and https://www.testsite/company/private-page

    So I tried to change the $url = home_url(‘/members-only/’); in $url = get_previous_posts_link(); because the user can login both from https://www.testsite.com/private-page and https://www.testsite/company/private-page.

    But in this case the redirect drive to the backend profile.

    hi fibbo8 –

    No problem I’m glad that snippet worked for you. I think I understand what you are trying to do, there is a great thread that I’ve linked below that should help you depending upon the setup of your login form. The solution will vary slightly depending on if you are hard coding your login form, linking to the standard WordPress login form, or embedding the standard WordPress login form on a different page. Please read over the thread linked below and implement the solution that fits your situation. If you have any questions please let me know.

    Link to thread outlining how to send a user back to the previous page after login: https://wordpress.stackexchange.com/questions/16004/redirect-user-to-original-url-after-login

    Thread Starter fibbo8

    (@fibbo8)

    Hy Belly,

    thanks again for your reply. You’ve been helpful.
    I’m not a developer and I’ve got to arrive to some results with many tries, but now it seems that redirects work like I’ve expected.

    I’ve follow the cue of your linked post, but made a little change.

    In my case the redirect worked if I defined the “href” omitting the $_SERVER[“HTTP_HOST”] paramater:

    <a href="<?php echo wp_login_url($_SERVER['REQUEST_URI'] ); ?>"

    To maintain the redirect on backend dashboard for the admin I’ve put this function in function.php:

    function my_login_redirect( $url, $request, $user ){
        if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
            if( $user->has_cap( 'administrator' ) ) {
                $url = admin_url();
            }
        }
        return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );
    

    hi fibbo8 –

    No problem at all, I’m glad my replies were helpful and that you were able to get the redirect working!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Quirk redirect after login’ is closed to new replies.