Monthly Archives: May 2021

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.