Hi Brajesh,
I’ve already posted on buddypress.org about this without a satisfactory response – and I’ve searched all over for it – but I can’t find a solution anywhere.
Quite simply, I want to redirect from one BuddyPress dynamic URL to another (in my particular case, from /members/%username%/mentions/ to /members/%username%/). I’d normally do this with htaccess, but the dynamic nature of BuddyPress’ member URLs makes this impossible. How do I do this simple thing?
Hi Lee,
In case of BuddyPress, you will have to know a few functions. I will add a few example belowbp_is_user() -> is used to check if we are on a user page
bp_is_user_[component nanme]<- Replavce with actual component to check if we are on a user component. e.g. bp_is_user_activity() bp_is_user_groups() bp_is_user_messages() etc To get the id of the user whose profile we are viewing $user_id = bp_displayed_user_id(); We can also check which sub nav of the current component a user is viewing using bp_current_action() and bp_is_current_action() To redirect, you can use bp_core_redirect( $url ) Now, after looking at these functions, we can write the following code for your above need.
function buddydev_custom_redirect() { //step 1, check if we are on a page that needs the redirection //This can be handled in two ways, ////depending on how many redirect you want to add for various pages. //if the number of redirects are too many, ////It is helpful to use negative logic like //// if( not_my_page ) { return ;} //for this simpel case if ( bp_is_user_activity() && bp_is_current_action( 'mentions' ) ) { $redirect_to = bp_core_get_user_domain( bp_displayed_user_id() );//profile bp_core_redirect( $redirect_to ); } } add_action( 'bp_actions', 'buddydev_custom_redirect', 0 );
I hope it helps.
- This reply was modified 8 years, 4 months ago by Brajesh Singh.
Oops, I made a mistake in my question! I meant the default activity tab, but your code allowed me to figure it out anyway:
function buddydev_custom_redirect() { if ( bp_is_user_activity() && bp_is_current_action( 'mentions' ) ) { $redirect_to = ( bp_core_get_user_domain( bp_displayed_user_id() ) . bp_get_activity_slug() . '/' ); bp_core_redirect( $redirect_to ); } } add_action( 'bp_actions', 'buddydev_custom_redirect', 0 );
One thing I’ve always wondered: why do some functions have a number at the end? What does it do?
Hi Lee,
You mean something like thisadd_action( 'bp_actions', 'buddydev_custom_redirect', 0 );
It is a function call and based on the definition of the function being called, a developer can pass the parameters.
In case of add_action/add_filter functions, the definition allows two optional numeric parameters as the last 2 . Example
add_action( 'some_action_name', 'some_callback', 'optional_numeric_priority', 'optional_expected_parameters by the callaback');
The fist numeric parameter tells us the priority of the attached callback function and the second numeric tell us about the expected number of parameters
Hi Brajesh,
Assuming the function is correctly written and unique, are the numbers really necessary? Do they just help in the case of potentially competing functions, or do they pass useful information anyway? (The answer to this one may go over my head.) And most importantly, can the wrong number break an otherwise sound function?
You must be logged in to reply to this topic.