Skip to:
Content
Pages
Categories
Search
Top
Bottom

Programmatically create xprofile field


  • ibes
    Participant

    @ib4s

    Hi there,

    I try to make sure that there is a certain xprofile field existing.

    It seems like that is a tough thing to do with BuddyPress, right?

    I am used to ACF where registering fields is quite easy.

    If I get it right xprofile fields are registered directly in the database.
    So the “xprofile_insert_field” function tries to write a line in the database.

    That means that I can not guarantee that a field has a certain ID and so I would need to focus on the name.
    But the name seems to also be the Title of the field. So if I need to change the title of the field everything falls apart. Do I see that correctly?!

    I now plan to give the field a defined id and maybe overwrite the field.
    Which is very messy and just works if I do huge id jumps for every field that i need, so that field that are manually created will not be missed.

    Is there a clean way to set up fields programmatically?
    We deploy our site with a tool and so the wish is that eg. a new staging site can be created without manual work.
    Also fields should be updated through code not manually.
    So if the Title of the field needs to be changed I would like to have that updated through code.

    Hm – should I use some kind of databank migration thing for that?

    Maybe you have tipps for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @ibes-

    You can get the profile field groups and fields by using the function:

    $groups = bp_xprofile_get_groups( array( 'fetch_fields' => true ) );

    With this information, you should be able to determine whether a field exists and what its ID is.

    Best,

    -David


    ibes
    Participant

    @ib4s

    That seems to be super helpful, thanks.

    Put still a field “name” is also the field title, right?

    So if I would try to make the name very unique.
    Like “unique45myField” this would be something that is user-facing, right?

    As the ID can be different in each installation I should rely my system on the field name. But if the field name is visible to the user, I should not go wild with it.
    I feel a bit stuck in this thought loop.

    Are the plugins who add xprofile fields to BuddyPress? How are they doing it?
    Could it make sense to overwrite the field name for display via a filter or so?

    That filter could do it:
    bp_get_the_profile_field_name

    Then I could work internally with a very unique field name and just overwrite it for display.
    Dirty workaround but would likely give me better sleep 🙂

    Or is xprofile anyway not the best idea and there is another, more suitable way to add profile fields to BuddyPress profiles?

    Hi @ibes-

    I’d try to use Xprofile for managing profile fields. There’s a lot of baked-in goodness with the Xprofile component.

    Obviously, I don’t know what will work in your situation, but it seems like name matching is reasonable. You could probably also extend the Xprofile Groups and Fields classes to add some sort of persistent-ID parameter for each object.

    Check out the code at bp-xprofile/classes/.


    nathan913
    Participant

    @nathan913

    One of the heavily requested features by BuddyPress users was the ability to add Repeater Fields or Repeater groups of fields dynamically to BuddyPress Profile, without having a predefined number Official Site

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar