BuddyDev

Search

[Resolved] BuddyPress Private Message for Non Logged in Users

  • Participant
    Level: Initiated
    Posts: 5
    arnotzon on #27351

    Hi there!

    I tried your code to display private message button on user profile even if the visitor is not logged in but it doesn’t work for me. The button is there but It sends back to a 404 url (“/compose/?r…”)

    Code is the following:

    /**
     * Filter on the private message button args to se it visible for
     * non logged in members. Also, setup the url for redirecting to login page
     *
     * @param mixed $args
     *
     * @return mixed
     */
    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' );

    Did something change with latest updates?

    Thanks for your help

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2875
    Ravi on #27352

    Hello,

    Thank you for posting. It seems you are using Noveau template pack. Please try the following code.

    
    /**
     * Modify private message button args.
     *
     * @param array $buttons Array.
     *
     * @return array
     */
    function buddydev_nouveau_filter_private_message_button( $buttons ) {
    
    	if ( ! isset( $buttons['private_message'] ) || is_user_logged_in() ) {
    		return $buttons;
    	}
    
    	$buttons['private_message']['must_be_logged_in']   = false;
    	$buttons['private_message']['button_attr']['href'] = wp_login_url( site_url( '?message-redirect=' . bp_displayed_user_id() ) );
    
    	return $buttons;
    }
    
    add_filter( 'bp_nouveau_get_members_buttons', 'buddydev_nouveau_filter_private_message_button' );
    
    /**
     * 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' );
    
    

    Let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 5
    arnotzon on #27458

    This worked like a charm, thank you so much Ravi 🙂

    You might want to update this page for others: https://buddydev.com/buddypress-private-message-non-logged-users/

    Cheers

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2875
    Ravi on #27459

    Thank You for the acknowledgement. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] BuddyPress Private Message for Non Logged in Users’ is closed to new replies.

This topic is: resolved