Reply To: Adding profile fields to members directory
Thank you to all who have helped and especially @danbp who took time to explain things at length.
I have finally grasped how this thing works, more or less.
I do need to apply the same quite complex filter to both the profile and directory list. I have now devised a common filter function plugin that can be invoked within the members loop as a filter and within the profile display as an action. I have to call it in different ways, depending on where it is being called from, and the trick was to call it from two separate hookable functions. The core of what I have follows:
// Remove standard filtering of profile fields
function ovni_init() {
remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 );
}
add_action( 'bp_init', 'ovni_init' );
// add our replacement filter, which is given a field value, to the members loop
function ovni_filter_field_for_directory($field_value){
$field_name=bp_get_the_profile_field_name();
return ovni_get_field_searchable($field_name);
}
add_filter( 'bp_get_the_profile_field_value', 'ovni_filter_field_for_directory', 50, 1 );
// add fields to directory listing make them searchable according to rules of this plugin
function ovni_add_info_to_members_loop() {
echo '<div style="position: absolute; left:60px;">', ovni_get_field_searchable('Ovni model'),'</div>';
echo '<div style="position: absolute; left:100px;">', ovni_get_field_searchable('Rig'),'</div>';
echo '<div style="position: absolute; left:275px;">', ovni_get_field_searchable('Home waters'),'</div>';
echo '<div style="position: absolute; left:500px;">', ovni_get_field_searchable('Home port'),'</div>';
}
add_action( 'bp_directory_members_item', 'ovni_add_info_to_members_loop' );
// the fuction that makes fields searchable according to the rules of this plugin
function ovni_get_field_searchable($field_name) {
global $ovni_no_link_fields, $ovni_social_networking_fields;
$field_value = xprofile_get_field_data($field_name, bp_get_member_user_id() );
if ( $field_value) { // only if there is content in the field
if (is_array($field_value)){
etc. etc.
I have not included the code of my filter, but it is changing the way text in profile fields are turned into links, both when displayed in the directory entry and in the members list, thus:
(1) If is an array, serialise into a comma-separated string
(as used in outputing which social web sites a mobile number can be used for)
(2) If is an image, change HTML so that clicking on image will open full size in new tab/window
(3) If any field contians text in [ ], the contents of the [ ] pairs are turned into separate directory searches.
So if the entire text is in [ ] it overrides all other rules
(4) Fields in the list of ‘do not link’ fields are left plain
(5) The user’s web site is converted to its URL
(6) Fields containing social network user names are turned into links to those networks
(7) The remaining are turned into links to search the directory for matches.
It has been a learning curve. My formative programming of this type was in the language B (the antecedent of C), which shows how far back I go!
thanks again for the help, Tony