BuddyDev

Search

[Resolved] Private message button for non logged in users

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28016

    Hello
    I want to show the private message button for non logged in users as well (BP Version 5.12).
    Therefor I used this code snippet:

    function buddydev_filter_private_message_button_args( $args ) {
    
    	//if the user is logged in, we don't need to do anything
    	//Also, I am only handling it for profile buttons here
    	if ( is_user_logged_in() || ! bp_is_user() ) {
    		return $args;
    	}
    
    	$user_id = bp_displayed_user_id();
    
    	$args['must_be_logged_in'] = false; //show for non logged in too
    	//and redirect to our custom url containing the user id
    	$args['link_href'] = wp_login_url( site_url('?message-redirect=' . $user_id ) );
    	return $args;
    }
    add_filter( 'bp_get_send_message_button_args', 'buddydev_filter_private_message_button_args' );
    
    /**
     * Redirect to the send message when the user logs in
     *
     *
    function buddydev_redirect_to_send_message_on_login() {
    
    	//make sure it is our url set in previous step
    	if ( ! is_user_logged_in() || empty( $_GET['message-redirect'] ) ) {
    		return ;
    	}
    
    	//redirect to PM
    	$user_id = absint( $_GET['message-redirect'] );
    	//let us make sure that user exists
    	$user = get_user_by('id', $user_id );
    	//can not PM to invalid user
    	//also you should not send message to yourself
    	if ( ! $user || $user->ID == bp_loggedin_user_id() ) {
    		return ;
    	}
    
    	$url =  bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . $user->user_login ;
    
    	bp_core_redirect( $url );
    }
    add_action( 'bp_template_redirect', 'buddydev_redirect_to_send_message_on_login' ); 

    The button shows up but on click the website url is missing and it comes out something like this “http://messages/compose?r=anbieter3” redirect to nowhere. “anbieter3” is the user profile where I click the message button.
    Some body can help me out, why the code dont read out the right site_url?

    Thanks very much. Frank

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #28022

    Hi Frank,
    Thank you for posting here.

    There is a bug in BP Nouveau template that cases it.

    I have updated the code with the fix. Please use the following code

    
    function buddydev_filter_private_message_button_args( $args ) {
    
    	//if the user is logged in, we don't need to do anything
    	//Also, I am only handling it for profile buttons here
    	if ( is_user_logged_in() || ! bp_is_user() ) {
    		return $args;
    	}
    
    	$user_id = bp_displayed_user_id();
    
    	$args['must_be_logged_in'] = false; //show for non logged in too
    	//and redirect to our custom url containing the user id
    	$args['link_href'] = wp_login_url( site_url( '?message-redirect=' . $user_id ) );
    
    	return $args;
    }
    
    add_filter( 'bp_get_send_message_button_args', 'buddydev_filter_private_message_button_args' );
    
    /**
     * Fix for nouveau.
     */
    add_filter( 'bp_nouveau_get_members_buttons', function ( $buttons ) {
    	if ( ! isset( $buttons['private_message'] ) ) {
    		return $buttons;
    	}
    
    	$buttons['private_message']['button_attr']['href'] = wp_login_url( site_url( '?message-redirect=' . bp_displayed_user_id() ) );
    
    	return $buttons;
    } );
    
    /**
     * Redirect to the send message when the user logs in
     **/
    function buddydev_redirect_to_send_message_on_login() {
    
    //make sure it is our url set in previous step
    	if ( ! is_user_logged_in() || empty( $_GET['message-redirect'] ) ) {
    		return;
    	}
    
    //redirect to PM
    	$user_id = absint( $_GET['message-redirect'] );
    //let us make sure that user exists
    	$user = get_user_by( 'id', $user_id );
    //can not PM to invalid user
    //also you should not send message to yourself
    	if ( ! $user || $user->ID == bp_loggedin_user_id() ) {
    		return;
    	}
    
    	$url = bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . $user->user_login;
    
    	bp_core_redirect( $url );
    }
    
    add_action( 'bp_template_redirect', 'buddydev_redirect_to_send_message_on_login' );
    

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28031

    Thanks Brajesh, it works like it should. Super. But since I do not use the default wp-login, but a seperate page (working with s2member and aiax-login) could you tell me where can I put a snippet to redirect to my login page (named: login.php) instead to the wp-login???
    Would be a big help.

    Best greatings, Frank

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28036

    Hey again.
    I resolved the problem with an redirect form the wp-login to my-login page. It might be not so beautiful but it works.
    Thanks again.

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28043

    Now I checked out, that if I am logged in I get also redirected to the logged in page, when I push the private message button. Something is wrong for logged in users now.

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28044

    Now I checked out, that if I am logged in I get also redirected to the logged in page, when I push the private message button. Something is wrong for logged in users now.

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #28045

    Hi Frank,
    There was an issue with my fix for Nouveau. I missed to check for logged in user.

    Here it is updated.

    
    
    /**
     * Fix for nouveau.
     */
    add_filter( 'bp_nouveau_get_members_buttons', function ( $buttons ) {
    	if ( is_user_logged_in() || ! isset( $buttons['private_message'] ) ) {
    		return $buttons;
    	}
    
    	$buttons['private_message']['button_attr']['href'] = wp_login_url( site_url( '?message-redirect=' . bp_displayed_user_id() ) );
    
    	return $buttons;
    } );
    
    /**
     * Redirect to the send message when the user logs in
     **/
    function buddydev_redirect_to_send_message_on_login() {
    
    //make sure it is our url set in previous step
    	if ( ! is_user_logged_in() || empty( $_GET['message-redirect'] ) ) {
    		return;
    	}
    
    //redirect to PM
    	$user_id = absint( $_GET['message-redirect'] );
    //let us make sure that user exists
    	$user = get_user_by( 'id', $user_id );
    //can not PM to invalid user
    //also you should not send message to yourself
    	if ( ! $user || $user->ID == bp_loggedin_user_id() ) {
    		return;
    	}
    
    	$url = bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . $user->user_login;
    
    	bp_core_redirect( $url );
    }
    
    add_action( 'bp_template_redirect', 'buddydev_redirect_to_send_message_on_login' );
    

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Frank Schumann on #28063

    Thank you Brajesh. It works perfect now. I appreciate your fast help alot.
    Have a nice day. Frank

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #28074

    You are welcome.
    I am glad it worked.

    Regards
    Brajesh

The topic ‘ [Resolved] Private message button for non logged in users’ is closed to new replies.

This topic is: resolved