BuddyBoss, Development

Updated on

How to add BuddyBoss private messaging to your directory

Alejandro Schmeichler

When using JReviews with BuddyBoss, or BuddyPress, you might want to add the ability for site members to contact listing owners directly from the directory listings. JReviews does include listing inquiries functionality out-of-the-box, and it allows anyone to contact listings owners using a contact form and the message is delivered via e-mail. However, if you prefer to keep all member communications in-site using BuddyBoss' private messaging system, then this is a great integration you can use.

To add this functionality we'll use the JReviews developer hooks, more specifically the listing_detail_action_buttons filter hook, which allows you to add more action buttons to the listing detail page.

To get started, if you are not already using hooks on your site, create the following file:

  • Joomla: SITE-ROOT/jreviews_overrides/filters/filter_functions.php
  • WordPress: SITE-ROOT/jreviews_overrides/filters/filter_functions.php

Add the following lines at the beginning of the file:

<?php
defined('MVC_FRAMEWORK') or die;

The code for the filter needs check a couple of things:

  • Is BuddyPress running? If not, there's no need to show the button.
  • Is the current site visitor logged in? If not, we can show a button that loads a sign-up modal.

Check if BuddyPress is running

To check if BuddyPress is running we can check if one of the functions we'll be using to generate the messaging link exists:

if (! function_exists('bp_loggedin_user_domain')) {
   return $buttons;
}

Checking if the current site visitor is logged in

We can check if the current user is a guest in a number of ways, but in this example we'll use a method that works for both Joomla and WordPress and relies on the JReviews framework. We check if the user is a guest, and if it is we build a button that will load the sign-up dialog in JReviews.

if (fwd_auth()->user()->isGuest()) {
	$url = fwd_request()->createFromGlobals()->fullUrl();
	
	$formData = json_encode(['current_url' => base64_encode($url)]);
	
	$messageButton = '&l;a href="/jreviews/users/signupModal" 
			class="jrButton jrSmall jrButton" 
			rel="nofollow noopener" 	
			data-jr-action="dialog" 
			data-jr-dialog-modal="1" 
			data-jr-width="800" 
			data-jr-title="Requires an Account" 
			data-form-data=\''.$formData.'\'
		>
			<span class="jrIconMessage"></span> Send Message
		';

	$buttons[] = $messageButton;

	return $buttons;
}

Generating the BuddyBoss message button

If BuddyBoss is active and the user is logged in, we can generate the BuddyBoss message button that will open the messaging system with the listing owner already added as the recipient.

// Get the current listing array
$listing = $params['listing'];

// Get the listing owner ID
$userId = $listing['User']['user_id'];

// Build the BuddyBoss/BuddyPress message link
$url = wp_nonce_url( bp_loggedin_user_domain().bp_get_messages_slug().'/compose/?r='.bp_core_get_username($userId));

// Generate the button
$messageButton = '
	<a href="'.$url.'" class="jrButton jrSmall">
	<span class="jrIconMessage"></span> Send Message
	</a>
';

$buttons[] = $messageButton;

Putting it all together with the listing_detail_action_buttons filter

The final code to add to the filter_functions.php file looks like this:

function add_bb_message_button($buttons, $params)
{
	if (! function_exists('bp_loggedin_user_domain')) {
		return $buttons;
	}

	// If user not logged in, show sign-up dialog
  	if (fwd_auth()->user()->isGuest()) {
		$url = fwd_request()->createFromGlobals()->fullUrl();
		
		$formData = json_encode(['current_url' => base64_encode($url)]);
		
		$messageButton = '<a href="/jreviews/users/signupModal" 
				class="jrButton jrSmall jrButton" 
				rel="nofollow noopener" 	
				data-jr-action="dialog" 
				data-jr-dialog-modal="1" 
				data-jr-width="800" 
				data-jr-title="Requires an Account" 
				data-form-data=\''.$formData.'\'
			>
				<span class="jrIconMessage"></span> Send Message
			</a>';

		$buttons[] = $messageButton;
 
		return $buttons;
	}

	$listing = $params['listing'];
	
	$userId = $listing['User']['user_id'];

	$url = wp_nonce_url( bp_loggedin_user_domain().bp_get_messages_slug().'/compose/?r='.bp_core_get_username($userId));

	$messageButton = '
	   <a href="'.$url.'" class="jrButton jrSmall">
		<span class="jrIconMessage"></span> Send Message
	   </a>
	';
 
	$buttons[] = $messageButton;
 
	return $buttons;
}
 
Clickfwd\Hook\Filter::add('listing_detail_action_buttons', 'add_bb_message_button', 10);

You can modify the CSS class attribute in the buttons if you want to style them differently, and also change the Send Message text to something that's more specific to your site.

And that's it. In a few lines of code and taking advantage of the JReviews developer hooks, we can integrate the BuddyBoss and BuddyPress messaging system with the JReviews listing detail pages.