• Resolved chrishechler

    (@chrishechler)


    Hi, thanks for your plugins.

    I was playing around with the BP search plugin in conjunction with BP xprofile fields and could create a nice country dropdown list for the search form (users can search other members via country list as a dropdown).

    Unfortunately, I could not find the same dropwdown option for the “city” field. So far, what I have created is either a regular search field to search all profile fields for “city” or the “geolocation” option. However, for these fields a user needs to manually enter the city he/she is looking for. I was hoping that the search field could be in a dropdown like the country search field.

    Thus, is it possible to create a dropdown with all cities of all members just like the country field?

    That would be really great.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Andrea Tarantini

    (@dontdream)

    Hi Chris,

    Do your members have a profile field where they enter their city? What field type is that field?

    Thread Starter chrishechler

    (@chrishechler)

    Hi, thank you for your reply, I really appreciate it ??

    I was hoping that there was a dropdown solution for cities the same way there is one for countries. I have come across websites that implement something like a nested way of showing this: first a user selects a country from dropdown and then a second dropdown appears with city options related to that specific country.

    Another possible solution could have been to have a dropdown for cities that list only the cities that members on the website have entered while filling out their profile page (BuddyBoss). This would be a selective pre-populated kind of solution.

    To answer your question: I have tried several ways to implement a profile field where members enter their city so that the city can then be searched in BP search form:

    1; create own dropdown (and populate the dropdown with city options which is far from optimal as you would need to enter hundreds of cities manually).

    2;use a simple single line text field (member needs to enter city manually)

    3.geolocation field (which unnecessarily mentions state and country, as I already have acountry field)

    Unfortunately, the fields do not all work with one another (interdependency). For example, If I use geolocation in the profile registration (so city, state and country are given) but want a country dropdown in the search form, the country dropdown does not find the user. The only way to find the user is to use a “any type field” where a user would have to manually start typing the country name. So, the only way to search for city is by having a user manually enter the city name.

    Using geolocation for both profile page and search form can become quite costly (depending on bots and number of users).

    Long story short, I was hoping there to be a more straightforward dropdown solution, to have a more user-friendly and faster way for users to search for other users by city. Best case scenario, I could have a dropdown for cities that show only the cities that users have entered in their profiles.

    That would be really awesome ??

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi Chris,

    Thanks for your detailed explanation!

    The following code turns a standard search field into a “dynamic” drop-down search field. Dynamic means that the drop-down list contains only the values selected by users in their profiles.

    add_action ('bps_field_before_search_form', 'bps_dynamic_dropdown');
    function bps_dynamic_dropdown ($f)
    {
    	global $wpdb;
    
    	$ids = array (2, 5, 9);  // IDs of your dynamic drop-down 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';
    }
    

    Enter your desired field IDs into the $ids array and please let me know if this code works on your site!

    Thread Starter chrishechler

    (@chrishechler)

    Hi Andrea,

    Sorry for thelate reply, I just had time to check your code.

    It is actually working, thank you sooo much!

    Maybe you can post the code on your website then others can use it too, if they are interested ??

    Again, thank you ??

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi Chris,

    You’re very welcome! I’m going to follow your suggestion and post the code on my website too.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Option to have dropdown for cities’ is closed to new replies.