All posts by andrea

Dynamic Dropdown Search Fields

The following code turns a standard BP Profile Search field into a dynamic dropdown search field. Dynamic means that the dropdown list contains only the values that users have actually selected in their profiles.

Let’s say your user profile contains a Country dropdown field with dozens of countries, but your users are only from a handful of them. When you add this code to your bp-custom.php file, the Country search field in your BP Profile Search form will only list the countries where at least one user can be found.

add_action ('bps_field_before_search_form', 'bps_dynamic_dropdown');
function bps_dynamic_dropdown ($f)
{
	global $wpdb;

	$ids = array (2, 5);  // IDs of the search fields that will become "dynamic dropdown" search fields
	if (empty ($f->id) || !in_array ($f->id, $ids))  return;

	$query = "SELECT DISTINCT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = {$f->id} ORDER BY value";
	$values = $wpdb->get_col ($query);
	$f->options = array ('' => '');
	foreach ($values as $value)
	{
		$value = stripslashes ($value);
		if ($value == '')  continue;

		$f->options[$value] = $value;
	}

	$f->display = (count ($f->options) == 1)? 'none': 'selectbox';
}

Please note that the above code does not work for Check Box and Multi-Select Box profile field types, because their values are stored in serialized form.

BP Profile Search 5.4.6

This new version of BP Profile Search adds a site information section to the admin interface. This information will be very useful if you ever post a request in a BP Profile Search support forum, either:

https://wordpress.org/support/plugin/bp-profile-search/

or:

https://dontdream.it/support/forum/bp-profile-search-forum/

When you have a support request, go to:

Tools -> Site Health -> Info -> BP Profile Search

You will find some lines like these:

Version			5.4.6
Platform		BuddyPress 10.3.0
Theme			Twenty Thirteen 3.5
Members index template	/plugins/buddypress/bp-templates/...
Members loop template	/plugins/buddypress/bp-templates/...
Form template(s)	/plugins/bp-profile-search/templates/...

Copy those lines and paste them into your support request, and most of the times I’ll be able to answer your question more quickly.

Exclude users from search results

If you wish to search your Members directory with BP Profile Search and exclude some users from your search results, you can use one of the following code snippets.

Let’s say you wish to remove the current user from their search results. Add this code to your bp-custom.php:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$donotshow[] = bp_loggedin_user_id ();
	$users = array_diff ($users, $donotshow);

	return $users;
}

If you wish to exclude the administrator(s) from search results, add this code to your bp-custom.php:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$args = array ('role' => 'administrator');
	$admins = get_users ($args);
	$donotshow = wp_list_pluck ($admins, 'ID');
	$users = array_diff ($users, $donotshow);

	return $users;
}

You can even combine the above snippets. The following code excludes both the administrator(s) and the current user from search results:

add_filter ('bps_search_results', 'bps_exclude_users');
function bps_exclude_users ($users)
{
	$args = array ('role' => 'administrator');
	$admins = get_users ($args);
	$donotshow = wp_list_pluck ($admins, 'ID');
	$donotshow[] = bp_loggedin_user_id ();
	$users = array_diff ($users, $donotshow);

	return $users;
}

Enabling Select2

If you wish to enable Select2 in your BP Profile Search forms, add this code to your bp-custom.php:

add_action ('bps_before_search_form', 'bps_enable_select2');
add_action ('bp_ps_before_search_form', 'bps_enable_select2');
function bps_enable_select2 ($F)
{
	wp_enqueue_script ('select2-js', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js', array ('jquery'));
	wp_enqueue_style ('select2-css', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css');
?>
	<script>
		jQuery(function ($) {
			$('.bps-selectbox select').select2({width: '10em', dropdownAutoWidth: true});
			$('.bps-multiselectbox select').select2({width: '10em', dropdownAutoWidth: true});
		});
	</script>
<?php
}

Please note that there are two calls to select2(), one to enable Select2 for standard drop-down fields (line 10), and the other to enable it for multi-select fields (line 11). If you aren’t interested in both, simply delete the line you don’t need.

The JavaScript object inside a select2() call contains your Select2 configuration options, and you can change them according to your needs. You can find the list of all the Select2 configuration options in:

https://select2.org/configuration/options-api

Taxonomy Search

Thanks to Mark Haygen and Tracy Haygen for letting me test this feature on their site CrewCall.community. The site will be ready for live use in June 2021.

If you are using BuddyPress Xprofile Custom Field Types and wish to search a Custom Taxonomy Multiselector field, you need to register that field type with BP Profile Search before you can add it to your search forms.

To do that, add this code to your bp-custom.php:

use BPXProfileCFTR\Field_Types\Field_Type_Multi_Select_Taxonomy;

add_action ('bps_custom_field', 'bps_register_msct');
add_action ('bp_ps_custom_field', 'bps_register_msct');
function bps_register_msct ($f)
{
	if ($f->type != 'multiselect_custom_taxonomy')  return;

	$taxonomy = Field_Type_Multi_Select_Taxonomy::get_selected_taxonomy ($f->id);
	$terms = get_terms ($taxonomy, array ('hide_empty' => false));

	$f->format = 'set';
	$f->get_value = '';
	foreach ($terms as $t)
		$f->options[$t->term_id] = $t->name;
}

add_action ('bps_field_before_search_form', 'bps_change_display');
add_action ('bp_ps_field_before_search_form', 'bps_change_display');
function bps_change_display ($f)
{
	if (isset ($f->type) && $f->type == 'multiselect_custom_taxonomy')
		$f->display = 'multiselectbox';
}

A general explanation on how to register custom field types is in:

Search by Custom Profile Field Types

Swap the labels

If a profile field is named, say, “My Job Title”, when you add that field to a BP Profile Search form you’ll probably need to change its label to a more general “Job Title” to avoid any confusion.

If users see “My Job Title” in a search form, they may think they have to enter their own job title! But of course they have to enter the job title they are looking for, and they will get the list of users with that job title.

An especially tricky case happens with some dating sites, where two profile fields are:

I am a: (Man/Woman) and
I am looking for a: (Man/Woman)

When you add those two fields to a search form without changing their labels, users doing the search will most likely enter their own gender into the “I am a” field, and their own preference into the “I am looking for a” field… and surely they will be disappointed by the search results they get.

To avoid this confusion, you can simply swap the labels of the two fields in your search form edit page, as follows (click to expand):

and your users will now get their expected search results.

BP Profile Search 5.3

To clean up the plugin code and simplify further development, version 5.3 no longer supports old form templates, see New form template structure.

To know if the templates you are using are supported in 5.3, look at the Template column in the admin page Users -> Profile Search. Templates whose name is shown in green or blue text are supported in 5.3, those shown in red text are not.

If you still need to use old form templates, keep using BP Profile Search 5.2.4. If you upgraded already, roll back to BP Profile Search 5.2.4, either manually or using the WP Rollback plugin.

In addition, the old shortcode [bps_display] has been removed. If you were still using it, you can now replace it with [bps_form].

Version 5.3 brings a few new features and bug fixes:

1. A search field can be marked as required

To do that, go to the Edit Form page and prefix the field Label with an asterisk, e.g.:

* City

If you prefer an alternative way to mark a field as required, you can add this code to your bp-custom.php file:

add_action ('bps_field_before_search_form', 'set_required');
function set_required ($f)
{
	if ($f->code == 'field_34')    // replace 34 with your field ID
	{
		// mark the field as required
		$f->required = true;
	}
}

2. A search field can be assigned a default value

To do that, add this code to your bp-custom.php file:

add_action ('bps_field_before_search_form', 'set_default');
function set_default ($f)
{
	if ($f->code == 'field_34')    // replace 34 with your field ID
	{
		// assign the default value
		$f->value = 100;
	}
}

3. A search field can be validated with custom rules

To do that, add this code to your bp-custom.php file:

add_filter ('bps_validate_field', 'validate_field', 10, 2);
function validate_field ($error_message, $f)
{
	if ($f->code == 'field_34')    // replace 34 with your field ID
	{
		// specify the field validation rule and error message
		if ($f->value <= 80)
			$error_message = 'Please enter a value > 80';
	}

	return $error_message;
}

Bug fixes

This version also fixes two bugs:

1. The pagination of search results in member directories not using AJAX;

2. A conflict with the group members template, resulting in the group members page not being displayed correctly sometimes.

The two bug fixes are also available in version 5.2.4; if you need them and can’t upgrade to 5.3, you can delete and then reinstall BP Profile Search 5.2.4.

BP Profile Search 5.0.4

With this version you can:

— Translate the keywords AND and OR used in searches. For instance, to translate the keywords in Spanish, add these definitions to bp-custom.php:

define ('BPS_AND', ' Y ');	// AND in Spanish
define ('BPS_OR', ' O ');	// OR in Spanish

— Adjust the multiple select size in search forms, using the bps_field_before_search_form hook. For instance, add this code to bp-custom.php:

add_action ('bps_field_before_search_form', 'new_multiselect_size');
function new_multiselect_size ($f)
{
	if ($f->display == 'multiselectbox')
	{
		$f->multiselect_size = 6;
	}
}

Additionally, a bug happening when you used the bps_match_all hook has been fixed, and some minor adjustments to the bps-form-default template have been made.

BP Profile Search 5.0.2

When you search your Members directory for City is: London, there is no doubt that all the members in your search results live in London. There is no need to show the value of the City field, or to order the search results by City.

But if you search for City is: London OR Paris, or for City contains: York, then you can’t say where a member in your search results actually lives. In these cases BP Profile Search 5.0.2 shows the value of the City field, and offers the option to sort your search results by City.

You can disable this feature if you don’t need it. Simply add this code to your bp-custom.php file:

add_filter ('bps_sort_options', 'change_sort_options');
function change_sort_options ($sort_options)
{
	$sort_options = array ();
	return $sort_options;
}

add_filter ('bps_details', 'change_details');
function change_details ($details)
{
	$details = array ();
	return $details;
}

The [bps_directory] shortcode allows you to activate this feature independently of the current search.

[bps_directory show='field_4,field_36' order_by='field_4,field_40']

shows the fields with ID 4 and 36 in all your directory (or search results) entries, and offers the option to sort your directory (or search results) by the fields with ID 4 and 40.

Please note that [bps_directory] is not a standard shortcode. You can enter it in a new page, and you’ll get a new Members directory, or you can enter it in the standard Members directory page, and you’ll be able to customize the standard Members directory itself.

BP Profile Search 5.0.2 also fixes a potentially nasty privacy problem. This involves a possible backwards incompatibility, see Potential privacy problem fixed in 5.0.2 for details.

And, last but not least, version 5.0.2 features a much improved compatibility with GEO my WordPress. Enjoy!

BP Distance Search

Following many user requests, today I’ve released BP Distance Search.

BP Distance Search adds a new Google Place Autocomplete field type to your BuddyPress extended profiles.

You can then create Google Place Autocomplete fields, and add them to your BP Profile Search forms, selecting either the new distance search mode or one of the usual text search modes contains, is, or is like.

I hope you’ll find this new plugin useful. For questions, suggestions and bug reports you can use the BP Profile Search Support Forum.