redirecting to user domain using $bp
-
Hi,
I create a function in bp-custom.php that does redirection and hooks to the login process.
after the function i am adding the action
add_action(‘wp_login’,’bp_location_redirect_to_page’,100,0);
the function is declaring global $bp and it seem to work fine except for one thing.
I am trying to create a redirection as follows:
$url = $bp->loggedin_user->domain . ‘profile/edit’;
bp_core_redirect($url);for some reason when this redirection is called it is redirecting to the home page and appending the ‘profile/edit’ to it without starting with the user domain (which is working fine for me in other places).
I know for sure that $bp is declared and working since I am using $bp->loggedin_user->id to run some conditions within the function.
Any help on this would be appreciated.Thanks,
Shai
-
You’re using the wrong action.
I think you should be looking at the “login_redirect” action.
Also you probably don’t want to redirect to the profile edit page on every single login, so a check should be made to see if (x) profile field is filled in, if it’s not, then continue with the login redirect.
I am testing and only sending to the profile in certain cases.
it does seem that the redirection is working when the user logs in but the value is wrong.
according to what you say it should be like this?
add_action(‘login_redirect’,’bp_location_redirect_to_page’,100,0);
??Yes, that’s right, but just curious why you have a “0” defined for the number of parameters being passed?
What does your full function look like?
i tried with the new action – same result.
here is the function:
function bp_location_redirect_to_page()
{
global $bp,$wpdb;$redirect_url_has_location = ‘http://athleticrash.com’;
$city = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=26” , $bp->loggedin_user->id ));
$state = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=27” , $bp->loggedin_user->id ));
$country = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=103” , $bp->loggedin_user->id ));
$zip = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=23” , $bp->loggedin_user->id ));
if (!$city || $city==”)
$has_city=0;
else
$has_city=1;if (!$state || $state==”)
$has_state=0;
else
$has_state=1;if (!$country || $country==”)
$has_country=0;
else
$has_country=1;if (!$zip || $zip==”)
$has_zip=0;
else
$has_zip=1;$user_link = $bp->loggedin_user->domain;
$profile_path = ‘profile/edit’;
$redirect_url_no_location = $user_link . $profile_path;if ($has_city==1 && $has_state==1 && $has_country==1 && $has_zip==1 )
bp_core_redirect($redirect_url_has_location);
else
bp_core_redirect($redirect_url_no_location);
}
/*Add an action to redirect user after login*/
add_action(‘login_redirect’,’bp_location_redirect_to_page’,100,0);Eek, you shouldn’t have to use $wpdb->get_var when there are a ton of template tags you could use like:
$city = xprofile_get_field_data( 26, $bp->loggedin_user->id );
Also you don’t need all those $has_field variables.
Just do a straight-out, also replace bp_core_redirect() with return:
if( !empty($city) && ..... )
return $redirect_url_has_location;Also since you’re not using any of the passed variables, just use:
add_action('login_redirect','bp_location_redirect_to_page',100);
well yeah – these i know and can fix easily but it still doesnt redirect to the right place for some reason
You shouldn’t use bp_core_redirect, you should be returning the $url. See my code above.
Made the changes and now it doesnt redirect at all as opposed to before when it did redirect but to the wrong place
Sorry! It should be add_filter, instead of add_action!
same result
Try this instead:
http://pastebin.com/M0N9d381and what do I put for the $user parameter from within bp-custom.php?
I mean what is passed as the $user parameter.
it seems that the 2 redirect parameters are not in use and the $user – i am not sure how it is passed here.
I have tried to do something similar (dont worry about the mess now – I just want the redirect to work):
function my_login_redirect($redirect_to,$redirect_to2,$user_id) {global $wpdb,$bp;
$user_id = $bp->loggedin_user->id;
$city = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=26” , $user_id ));
$state = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=27” , $user_id ));
$country = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=103” , $user_id ));
$zip = $wpdb->get_var($wpdb->prepare(“SELECT value FROM wp_bp_xprofile_data WHERE user_id=%d AND field_id=23” , $user_id ));
if (!$city || $city==”)
$has_city=0;
else
$has_city=1;
if (!$state || $state==”)
$has_state=0;
else
$has_state=1;
if (!$country || $country==”)
$has_country=0;
else
$has_country=1;
if (!$zip || $zip==”)
$has_zip=0;
else
$has_zip=1;
if ($has_city==1 && $has_state==1 && $has_country==1 && $has_zip==1 )
return ‘http://athleticrash.com’;
else
return bp_core_get_user_domain($user_id) . ‘profile/edit/’;
}
add_filter(‘login_redirect’,’my_login_redirect’,100,3);this gave the same results – just went to the home page.
In your code, $user_id doesn’t work because you’re not passing the actual user id.
$user_id->ID would work in this case.If you’re wondering what the $user variable is in the login_redirect filter, check it out:
http://adambrown.info/p/wp_hooks/hook/login_redirect?version=2.9&file=wp-login.phpAlso, have you tried my Pastebin code? It works. I tested it myself.
Just modify what I have there for your own needs.i now have this which is your code:
function my_login_redirect($redirect_to,$redirect_to2,$user) {
global $bp;
$city = xprofile_get_field_data(26, $user->ID );
$state = xprofile_get_field_data(27, $user->ID );
$country = xprofile_get_field_data(103, $user->ID );
$zip = xprofile_get_field_data(23, $user->ID );if( !empty($city) && !empty($state) && !empty($country) && !empty($zip) )
return ‘http://athleticrash.com’;
else
return bp_core_get_user_domain($user->ID) . ‘profile/edit/’;
}
add_filter(‘login_redirect’,’my_login_redirect’,100,3);
goes to the home page…
Put it in your theme’s functions.php file.
put it in bp-sn-parent functions.php – goes to home page
Just try one condition… one xprofile field, and see if it works.
Also make sure all the xprofile fields you’re checking are blank.It’s working fine for me.
OT: You’re using BP 1.1.3?
yes 1.1.3 and all the fields are blank – i deleted them on the DB
i tried to append $bp->loggedin_user->id to the url just to see what it is and it is 0 so none of the queries are correct too.
any reason why it would be 0 and not the right user id if i declaried global $bp etc?$bp->loggedin_user->id uses wp_current_user, which isn’t available at that stage of the login process.
The login_redirect filter uses wp_signon:
http://adambrown.info/p/wp_hooks/hook/login_redirect?version=2.9&file=wp-login.phpIf you use the passed $user variable, you should be able to echo something.
If you’re using your own code above, try echoing $user->ID.finally working!
same code as before but it turns out the filter didnt work because of modifications i made to wp-login.php
i reverted some of the change there for the filters to work and now the redirection is working.
thank you so much for your help!!!Shai
@shaisimchi – glad things finally worked!
- The topic ‘redirecting to user domain using $bp’ is closed to new replies.