Skip to:

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#5250 closed defect (bug) (no action required)

bp_send_message_button() doesn't get member id in members-loop.php

Reported by: sgr33n's profile sgr33n Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Core Keywords:



I'm adding a send message button to every user on the users directory page adding the following action:

add_action( 'bp_directory_members_actions', 'bp_send_message_button' );

Even if bp_member_add_friend_button works pretty good, bp_send_message_button doesn't get the destination user id, resulting in the link:

I suppose it's a bug, because the function bp_send_message_button() works good on the member-header.php.

Change History (4)

#1 @boonebgorges
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

This is expected behavior, though not very well documented.

bp_send_message_button() uses bp_get_send_private_message_link() to create its URL. That function, in turn, uses bp_displayed_user_id() to determine the ID of the "destination" user. On member profile pages - ie when using member-header.php - the "displayed user" is defined. But there is no single "displayed user" when you're in the loop on the members directory.

This is part of a larger pattern in BuddyPress: we have functions that are intended for use in loops, and those that are intended for use on single-item pages.

In this specific case, I don't think BP has a function for doing exactly what you're trying to do. So, I'd suggest creating your own. Just copy the content of bp_send_message_button() into your theme, change its name, and then change the way that the 'link_href' param is determined: use the logic from bp_get_send_private_message_link(), but use bp_get_member_user_id() instead of bp_displayed_user_id() (the former is the loop function for getting the ID of the current user).

#2 @sgr33n
11 years ago

Thanks Boonebgorgers,
So i will create a new function following your instructions :)

#3 @sgr33n
11 years ago

This is how looks like :)

 * Adds a new way to display the private message button, for inloop users. Ex. Users directory.
 * @since SGR WP13 Theme 1.0
function sgrbp_send_message_inloop_button() {
	echo sgrbp_get_send_message_inloop_button();

function sgrbp_get_send_message_inloop_button() {
	// Note: 'bp_get_send_message_button' is a legacy filter. Use
	// 'bp_get_send_message_button_args' instead. See #4536
	return apply_filters( 'sgrbp_get_send_message_inloop_button',
		bp_get_button( apply_filters( 'bp_get_send_message_button_args', array(
			'id'                => 'private_message',
			'component'         => 'messages',
			'must_be_logged_in' => true,
			'block_self'        => true,
			'wrapper_id'        => 'send-private-message',
			'link_href'         => sgrbp_get_send_private_message_inloop_link(),
			'link_title'        => __( 'Send a private message to this user.', 'buddypress' ),
			'link_text'         => __( 'Private Message', 'buddypress' ),
			'link_class'        => 'send-message',
		) ) )

function sgrbp_send_private_message_inloop_link() {
	echo sgrbp_get_send_private_message_inloop_link();

function sgrbp_get_send_private_message_inloop_link() {

	if ( bp_is_my_profile() || !is_user_logged_in() )
		return false;

	return apply_filters( 'sgrbp_get_send_private_message_inloop_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( bp_get_member_user_id() ) ) );

 * Add the Send message button to the directory members page.
 * @since SGR WP13 Theme 1.0
 * @return void
add_action( 'bp_directory_members_actions', 'sgrbp_send_message_inloop_button' );
Version 0, edited 11 years ago by sgr33n (next)

#4 @boonebgorges
11 years ago

Exactly! Hope it's working well for you. Thanks for sharing!

Note: See TracTickets for help on using tickets.