Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

dynamically create new sets of profile fields (13 posts)

Started 1 year, 10 months ago by: Roger Coathup

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 10 months ago:

    We want to be able to dynamically create new profile fields:

    e.g. we have a set of profile fields for employment: employer name, dates employed, etc.

    When the user enters one set of employment fields, we want to create another set, so they can enter a second employer, and a third employer and so on.

    Creating as many set of fields as the user needs for all his employers. (this is pretty much the way Facebook or LinkedIn works)

    It looks to me like we need either arrays of profile groups, or a way to dynamically create fields.

    Any thoughts on how to do this in BuddyPress?

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 10 months ago:

    bump! Anyone else tried to do this… it seems a reasonably fundamental thing to want to do with profile fields?

  • Profile picture of paulhastings0 paulhastings0 said 1 year, 10 months ago:

    Yeah, I would agree that a functionality like that would be nice.

  • Profile picture of Boone Gorges Boone Gorges said 1 year, 10 months ago:

    This could be done, but it would take some custom coding. Here’s how I would structure it.

    1) Write some JS for the Edit Profile page that inserts a new set of fields based on user input (say, a button that says “Add another employer”). This wouldn’t really require any BP code in and of itself.

    2) Hook a function to ‘xprofile_updated_profile’, which is a do_action hook called right after all of the xprofile fields are saved. Grab the $_POST data, and see if there are any items in that array for which no profile fields yet exist. Use xprofile_insert_field() to create the new fields (it might be like employer_2_employer_address – something that iterates nicely).

    3) On the front end, you’ll want to make sure that not all of the employer groups are shown to each user on the edit profile screen. By default, each user sees each profile field. That means that if one person on your system listed ten employers, then everyone would see ten employer field groups when they went to edit their profile. You could do this in the profile edit template, by checking bp_get_the_profile_field_name() against a list of permitted field names.

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 10 months ago:

    @boonebgorges – Great that you think it can be done with the existing tables (I was considering adding some additional tables keyed on the user profile id). The hook into xprofile at the right place is what I was looking for.

    We are going to filter out profile fields where the user hasn’t input a value anyway (and not display them). I take it this is as simple as checking whether the field value is an empty string. Although, as you point out, you would still want these displayed on the edit page.

    So, it’s check for NULL and check for standard field (vs. extended field) name as well.

  • Profile picture of Arx Poetica Arx Poetica said 1 year, 9 months ago:

    Just following up on this — what might some code look like? Say, to just even simply create a new profile field type and populate it once?

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 9 months ago:

    @arxpoetica – we haven’t implemented any code yet, as we may now be moving all profile field handling out of BuddyPress on this client project.

    If we do end up cutting BP code this, I’ll remember to post a snippet.

  • Profile picture of Natasha Natasha said 1 year, 7 months ago:

    Hello Roger, at the company where I work are asking me to do a development similar to your questions …. and you develop something?, suddenly you develop a plugin?

    Thank you very much!!!

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 7 months ago:

    @slashnata – no entiendo tu pregunta exactamente

  • Profile picture of Roger Coathup Roger Coathup said 1 year, 7 months ago:

    @arxpoetica -

    I don’t know if you found any code in the meantime, but in case you haven’t:

    The basic function to insert a new profile field is: xprofile_insert_field()

    And, to populate a field use: xprofile_set_field_data()

    They are defined in bp-xprofile.php

  • Profile picture of Natasha Natasha said 1 year, 7 months ago:

    Thank you very much for responding so fast!

    The company wants to develop with different profiles, for example for people in human resources, technology, etc. for different areas of the company.
    I’m looking at the function you gave me. My question was if suddenly you had already somewhat developed because i have little experience in PHP. :)

    Thank you very much.

  • Profile picture of nanchante nanchante said 1 year, 1 month ago:

    Did this ever get off the ground?

  • Profile picture of Andrés Richero Andres Richero said 1 year ago:

    I´m looking for the same kind of thing as Natasha but just different environment (university).
    Has anybody gone further on a real case (solution) and any piece of code that might be picked up as a starter.
    Thanks a lot.

    Andrés