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!
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!
Share Improve this question edited Sep 13, 2018 at 21:16 Peechaya asked Sep 13, 2018 at 11:43 PeechayaPeechaya 215 bronze badges3 Answers
Reset to default 1According 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);
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' );
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' );