redirect home page to another if user logged in php

Hey! I am wondering if you would like to help me with my php code. I want users to be redirected from home page to a custom page called "dashboard" if they are logged in. If not, then I want them to be able to enter the standard home page.
This is the code that I have come up with:

<?php if (is_user_logged_in() ) {
wp_redirect ( home_url("/dashboard") );
exit;
}
?>

I have written it in header.php of my themes child. At the very top. When I try this code ill get an error. Something like "this page try to redirect you to many times" or something like that.

I would really appreciate your support! tnx

  • MarcuSM
    • Site Builder, Child of Zeus

    I don't want login redirection, I want home page redirection if user is logged in. Do you understand?
    So if the user is logged in since last day, and if they enter my website with the home url they will be redirected to mywebsite.com/dashboard. If not, they will enter mywebsite.com.

  • Alex Stine
    • System Administrator

    Hello @support494,
    Please try this code in functions.php

    <?php
    /**
     * 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('/custom-page-here/');
            }
        }
        return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    Remember to replace my-custom-page-here with the ending part of the URL. For example, if my page URL was http://example.com/members, I would put members in between the 2 slashes.

    Hope this helps.

    Thanks,
    Alex :smiley:

  • MarcuSM
    • Site Builder, Child of Zeus

    Hello Alex! hm.. it don't seems to work :slight_frown: No redirection. I just get a white screen when trying to enter my home url. Is there any other way?
    I have search a little and this code should work, but doesnt..
    `<?php if (is_user_logged_in() ) {
    wp_redirect ( home_url("/dashboard") );
    exit;
    }
    ?>`

    Thanks
    Marcus

  • Alex Stine
    • System Administrator

    Hello,
    You cannot use <?php tags in functions.php, sorry about that. You can only use them if the current <?php tag is closed with ?>. Please try this code instead.

    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('/custom-page-here/');
            }
        }
        return $url;
    }
    add_filter('login_redirect', 'my_login_redirect', 10, 3 );

    Please let us know if this worked for you.

    Thanks,
    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello,
    Please try this code.

    function my_login_redirect( $redirect_to, $request, $user ) {
    
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    
    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {
    
    // redirect them to the default place
    return $redirect_to;
    } else {
    return home_url();
    }
    } else {
    return $redirect_to;
    }
    }
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

    Please let me know if this works for you.

    Thanks,
    Alex :smiley:

  • MarcuSM
    • Site Builder, Child of Zeus

    This code broke the site. btw, I should place the specific page like this right?

    function my_login_redirect( $redirect_to, $request, $user ) {
    
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    
    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {
    
    // redirect them to the default place
    return $redirect_to;
    } else {
    return home_url('/dashboard/');
    }
    } else {
    return $redirect_to;
    }

    Thank you Alex!

  • Alex Stine
    • System Administrator

    Hello,
    Please try this code instead, sorry not a coder.

    function my_login_redirect( $redirect_to, $request, $user ) {
    
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    
    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {
    
    // redirect them to the default place
    return $redirect_to;
    } else {
    $redirect = 'http://site.com/page/';
    }
    } else {
    return $redirect;
    }
    }
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

    Please let us know if this worked for you.

    Thanks,
    Alex :smiley:

  • Alex Stine
    • System Administrator

    Hello,
    Please try this code, I think I got it this time.

    `function my_login_redirect( $redirect_to, $request, $user ) {

    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {

    // redirect them to the default place
    return $redirect_to;
    } else {
    $redirect_to='http://site.com/page/';
    }
    } else {
    return $redirect_to;
    }
    }
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

  • Alex Stine
    • System Administrator

    Hello,
    I found this code and I think it will work just fine for you.

    function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
    $role = $user->roles[0];
    $dashboard = admin_url();
    $myaccount = get_permalink(2148);
    if( $role == 'administrator' ) {
    //Redirect administrators to the dashboard
    $redirect = $dashboard;
    }
    elseif ( $role == 'editor' ) {
    //Redirect editors to the dashboard
    $redirect = $dashboard;
    } elseif ( $role == 'author' ) {
    //Redirect authors to the dashboard
    $redirect = $dashboard;
    } elseif ( $role == 'customer' || $role == 'subscriber' ) {
    //Redirect customers and subscribers to the "My Account" page
    $redirect = $myaccount;
    	} else {
    		//Redirect any other role to the previous visited page or, if not available, to the home
    $redirect = wp_get_referer() ? wp_get_referer() : home_url();
    }
    return $redirect;
    }
    add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

    Be sure to change the number to your actual page id on this line.

    $myaccount = get_permalink(2148);
    Currently it is 2148, you can get your current page id from the URL bar while editing the page, you will see a number towards the end of the URL. Insert that number and you should be good to go.

    Please let me know if this works for you.

    Thanks,
    Alex :smiley:

  • Sajid
    • DEV MAN’s Sidekick

    Hi @support494,
    Hope you are doing good today :slight_smile:

    The code posted by Alex should work by looks of it. Please feel free to post a reply if you need further assistance.

    Thanks Alex for helping the member, sending some points your way :slight_smile:
    Cheers, Sajid

  • MarcuSM
    • Site Builder, Child of Zeus

    Hey WPMUDEV and Alex! The code that Alex wrote did not work. But I found another that worked great :slight_smile:

    function switch_homepage() {
    if ( is_user_logged_in() ) {
        $page = get_page_by_title( 'dashboard' );
        update_option( 'page_on_front', $page->ID );
        update_option( 'show_on_front', 'page' );
    } else {
        // different front page...
    }
    }
    add_action( 'init', 'switch_homepage' );

    I still want to thank you for your time. Alex, Since I am about to leave this forum I want to give you something nice. Look at your points :smiley: <3

    Cheers, Marcus

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.