BuddyDev

Search

[Resolved] A very simple thing, but I can't find how to do it anywhere

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #4475

    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?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #4479

    Hi Lee,
    In case of BuddyPress, you will have to know a few functions. I will add a few example below

    bp_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.

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #4488

    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?

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #4489

    Oh, I forgot to mention: it works perfectly. Thanks Brajesh, you’re a champion!

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #4510

    Hi Lee,
    You mean something like this

    
    add_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

  • Participant
    Level: Enlightened
    Posts: 66
    ljmac on #4511

    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.

This topic is: resolved