BuddyDev

Search

Nested Buddypress profile fields

  • Participant
    Level: Enlightened
    Posts: 146
    NikB on #30641

    I’m trying to find a way to allow a member to select their country from a list, then display a second list of cities (dependent on the selection in the country list) eg. USA gives a choice of New York, Chicago or Los Angeles, UK gives a choice of London or Edinburgh etc.

    There seem to be numerous requests for such a feature but so far I’ve not come across any workable solutions. Note: in my case, I’m working with a fairly limited number of countries/cities so I don’t mind having to hard code those if necessary, but what I DO need is essentially a way to dynamically update the second list based on the selection in the first.

    Any suggestions or even pointers in the right direction would be very much appreciated.

    With many thanks in advance.

  • Keymaster
    (BuddyDev Team)
    Posts: 21806
    Brajesh Singh on #30645

    Hi Nick,
    You can create a field type and use js to filter out the states list based the selected country.

    In past, when I did something similar for a client(I guess around 5-6 year ago), we kept the list of states indexed by country and then on the client side, kept rebuilding the states dropdown based on that.

    There can be multiple strategies for doing it clinet side. Do not forget to validate the selection server side to for correct state/country relation.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    NikB on #30680

    Hi Brajesh

    Thank you so much for your prompt and helpful reply. If I understand correctly, you’re suggesting creating two fields ie. one for countries and one for states, with each state having a connection to a specific country (presumably via xprofile_meta)?

    Then use js (on ready or change) to filter the relevant states…

    Hopefully that makes sense and will give it a try.

    Regards
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 21806
    Brajesh Singh on #30698

    Hi Nik,
    I am not suggesting creating 2 fields. That can be another strategy.

    Your main concern is having 2 select input fields. It does not matter where they come from 1 field type or 2 fields.

    And yes, using 2 fields will make it easier for you to filter via js.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    NikB on #30701

    Hi Brajesh

    Ah I’m not sure I’m entirely following you in that case. So you are suggesting to create one field TYPE which automatically generates 2 select input fields?

    If you say having 2 fields (the way I’ve described above) will make it easier to filter, then I may give it a try that way initially and if that doesn’t work, will come back and re-read what your suggestion again 😉

    With many thanks as always
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 21806
    Brajesh Singh on #30729

    Hi Nik,
    You can do it without new field type but I will recommend new field type. With new field type, you will have control over option key and labels(can have different value and labels in the dropdown), With an existing field like select box, we do not get that.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    NikB on #30752

    Hi Brajesh

    Thank you again. I’m not familiar with creating a new field type but would this then automatically generate (and populate) the two select boxes?

    I did come across this – https://gist.github.com/sbrajesh/5b3f4fd44c9c91b3ed3e – which looks like your work from some years ago (perhaps what you were referring to) but this caused an error when added to my bp-custom.php (ie. Class ‘BP_XProfile_Field_Type_Selectbox’ not found in C:\xampp\htdocs\mach2\wp-content\plugins\bp-custom.php). I’m guessing maybe I should look at how you have achieved similar with your BuddyPress Xprofile Custom Field Types plugin and follow a similar process?

    Regards
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 21806
    Brajesh Singh on #30759

    Hi Nik,

    When you create a field type, you have control over how the field renders.

    Yes, that code is from me. It seems I might have shared that for one of our members here.

    you can not put that code in bp-custom.php. Try putting it in your functions.php or loading after bp_loaded action.

    You may also take a look at the custom field types.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 146
    NikB on #30763

    Thank you again Brajesh.

    Putting it in the functions.php looks as if it will work although I am now getting the error “Declaration of BD_XProfile_Field_Type_Country::display_filter($field_value) should be compatible with BP_XProfile_Field_Type::display_filter($field_value, $field_id = ”) in… ” I’m guessing something must have changed since the code was written so will have a look at your custom field types to see if I can spot the difference 😉

    Regards
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 21806
    Brajesh Singh on #30765

    Hi Nik,
    Like the error says, you need to add another parameter in the function definition.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved