1

I'm a novice at Wordpress and i'm pretty sure what i'm looking for is really easy. I have an issue with a function that doesn't work :

    function user_homepage() {

    if ( is_user_logged_in() ) {
        $args = array(
            'field'   => 25,
            );
      $user_homepage = bp_profile_field_data( $args );

            if ($user_homepage == 'Sports') {
                $redirect_to = home_url('/sports/');
                wp_redirect( $redirect_to );
                exit;
            }

            else if ($user_homepage == 'Tech') {
                $redirect_to = home_url('/tech/');
                wp_redirect( $redirect_to );
                exit;
            }

    }
}
add_filter('login_redirect','user_homepage',10,3);

Updated version, still doesn't work

function redirect_user_homepage() {

        global $bp;

        if ( is_user_logged_in() && is_front_page() ) {
            $args = array(
                'field'   => 25,
                'user_id' => bp_loggedin_user_id()
                );
          $user_homepage = bp_profile_field_data($args);

                if ($user_homepage == "All") {
                    wp_redirect( home_url() );
                    exit;
                }

                elseif ($user_homepage == "Sports") {
                    wp_redirect( home_url('/sports/') );
                    exit;
                }

                elseif ($user_homepage == "Tech") {
                    wp_redirect( home_url('/tech/') );
                    exit;
                }
        }
    }
add_action( 'template_redirect', 'redirect_user_homepage');

As you can see i want to redirect my users based on a custom field i created in Buddypress. But more than that i don't want them to simply be redirected after login, i want them to be redirect to this page when they go to the website homepage.

Any help would be greatly appreciated! Thanks!

3 Answers 3

1

According to your functions, if you want to redirect users when they are visiting the website's homepage. You could implement the is_front_page() function to detect and only redirects if they are on the frontpage.

function user_homepage() {

    if ( is_user_logged_in() ) {
        $args = array(
            'field'   => 25,
        );

        $user_homepage = bp_profile_field_data( $args );

        if ( is_front_page() ) {

            if ($user_homepage == 'Sports') {
                $redirect_to = home_url('/sports/');
                wp_redirect( $redirect_to );
                exit;
            }

            else if ($user_homepage == 'Tech') {
                $redirect_to = home_url('/tech/');
                wp_redirect( $redirect_to );
                exit;
            }
        }
    }
}
add_filter('login_redirect','user_homepage',10,3);
1
  • Right! I didn't think of this function, thanks! Now i just have to figure out why the redirection is not happening...
    – Peechaya
    Sep 13, 2018 at 21:31
1

You are going to need two SEPARATE but SIMILAR functions. I have never used BuddyPress, so I have to guess that returns a string based on how your logic was written.

I tested this on a nearly new WordPress install and it worked for me by manually setting the $user_homepage variable as a default since I did not have BuddyPress installed. The function_exists helps protect against issues when the plugin is deactivated or missing.

A quick look at the bp_profile_field_data tells me it excepts a field name, so I would use that over a field id. It also implied it could/would get the current user so it probably doesn't need a user_id.

Happy coding.

Successful Login Redirect

/**
 * Redirect Users to Their Home Page Upon Successful Log in
 */
function my_login_redirect( $redirect_url, $request, $user ) {

    if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        $user_homepage = null;

        if ( function_exists( 'bp_profile_field_data' ) ) {
            $user_homepage = bp_profile_field_data( array(
                'field' => 'Field Name',
            ) );
        }

        if ( isset( $user_homepage ) ) {
            if ( $user_homepage === 'Sports' ) {
                $redirect_url = home_url( '/sports/' );
            }
            elseif ( $user_homepage === 'Tech' ) {
                $redirect_url = home_url( '/tech/' );
            }
        }
    }

    return $redirect_url;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Logged in User Front Page Redirect

/**
 * Redirect Users to Their Home Page When Hitting the Front Page
 */
function my_template_redirect() {

    if ( is_user_logged_in() && is_front_page() ) {
        $user_homepage = null;

        if ( function_exists( 'bp_profile_field_data' ) ) {
            $user_homepage = bp_profile_field_data( array(
                'field' => 'Field Name',
            ) );
        }

        if ( isset( $user_homepage ) ) {
            if ( $user_homepage === 'Sports' ) {
                wp_safe_redirect( home_url( '/sports/' ) );
                exit;
            }
            elseif ( $user_homepage === 'Tech' ) {
                wp_safe_redirect( home_url( '/tech/' ) );
                exit;
            }
        }
    }
}
add_filter( 'template_redirect', 'my_template_redirect' );
2
  • Thank you so much for your help! You're right i should use function_exists to prevent issues. Unfortunately even with manually setting $user_homepage the redirection is still not happening and i checked all my wp-redirect and nothing seems to be in conflict so i have no clue of why it doesn't work...
    – Peechaya
    Sep 14, 2018 at 10:25
  • If you manually set it after $user_homepage = null; AND the bp_profile_field_data function_exists than that will overwrite the manually set value; My thought is that perhaps $user_homepage returns a different value than you are expecting. Try debugging $user_homepage and make sure your if conditions Sports and Tech are accurate. (It could be a lowercase/uppercase issue) ... Try to debug the $user_homepage before if ( isset( $user_homepage ) ) { ... printf( "\n<pre class=\"debug\">\n%s\n</pre>\n", var_export( $user_homepage, true ) ); Sep 14, 2018 at 21:15
1

I forgot to update this topic. I've fixed my problem. Thanks again for the help!

Here's what worked for me:

/**
 * Redirect users to their personalized homepage
 */
function peechaya_user_homepage() {

    global $bp;

    if ( is_user_logged_in() && is_front_page() ) {

        if ( function_exists( 'bp_profile_field_data' ) ) {
                    $args = array(
                'field'   => 25,
                'user_id' => bp_loggedin_user_id()
                        );
            $user_homepage = bp_get_profile_field_data( $args );
        }

        if ( isset( $user_homepage ) ) {
            if ( $user_homepage === 'All' ) {
                /* Default action */
            }
            elseif ( $user_homepage === 'Tech' ) {
                wp_safe_redirect( site_url( '/tech/' ) );
                exit;
            }
            elseif ( $user_homepage === 'Sports' ) {
                wp_safe_redirect( site_url( '/sports/' ) );
                exit;
            }
        }
    }
}
add_action( 'template_redirect', 'peechaya_user_homepage' );

Your Answer

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

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