Skip to:
Content
Pages
Categories
Search
Top
Bottom

Create custom field in ’base’ group manually

  • @drrxa

    Participant

    I am new to BuddyPress and I would like to create custom field in the ‘base’ group through the BuddyPress hooks not through the profile fields admin interface. I have been searching for a tutorial or tips on how to do that but did not find any. I imagine that there would be a function to create that field that will look something like this:
    `
    bp_create_field($field_name, $group_id, $data_type, $required, $field_type, $options = array())
    `
    and then hooking this function to whatever hooks to dispaly and save that field on the backend and frontend of the website. is that possible? or there is another way to do that?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • @drrxa

    Participant

    My specific problem is that I need to add a country select box to the extended user profile that will appear in registration form and I don’t want to add it through the admin panel because (1) it is hard to enter 256 countries manually and (2) I will need multiple instances of that list.

    Now I came up with the following code after a while of digging into the core files of BuddyPress, the first part that add the ‘Country’ `selectbox` works fine, but the second part which is supposed to add the countries options does not wok, I get an empty `selectbox` that is labeled “Country” without any options:

    `
    add_action(‘bp_init’, ‘add_custom_country_list’); // That might not be the perfect hook to use
    function add_custom_country_list() {
    if (xprofile_get_field_id_from_name(‘Country’)) return;
    $country_list_args = array(
    ‘field_group_id’ => 1,
    ‘type’ => ‘selectbox’,
    ‘name’ => ‘Country’,
    ‘Description’ => ‘Please select your country’,
    ‘is_required’ => true,
    ‘can_delete’ => false,
    ‘order_by’ => ‘default’
    );
    $country_list_id = xprofile_insert_field($country_list_args); // It work till here – The following part does not work
    if ($country_list_id) {
    $country_list = array(‘USA’,’Germany’,’England’); // To be replaced with the full country list
    foreach ($country_list as $i => $country) {
    xprofile_insert_field(array(
    ‘field_group_id’ => 1,
    ‘parent_id’ => $country_list_id,
    ‘type’ => ‘option’,
    ‘name’ => $country,
    ‘option_order’ => $i+1
    ));
    }
    }
    }
    `
    Can anyone help me about what is wrong with this code? Thanks.

    @drrxa

    Participant

    I have a working code now, the following code is able to insert a select box profile field to the ‘base’ group with any long list of options that might be hard to insert manually through the user interface. this specific example will insert a profile field with the title of “Country” that contains all countries as options. Use this code in your `functions.php` or `bp-custom.php` file and you will notice a new profile field with the title of “Country” the next time you visit the Base group in Profile Fields admin area. You can then update and change the options to suit your needs and after that click “Save” button to register this field.

    Be aware that the following code will not add a new “Country” profile field as long as a profile field with the title of “Country” already exists, meaning that if you change the title to “List of Countries” for example, it will add a new profile field with the title of “Country” to your profile field, so I think it is better to use this code only once and whenever you need to insert a long listed profile field, and then remove the code from `functions.php` or `bp-custom.php` and keep on other place for future use.
    `
    add_action(‘bp_init’, ‘add_custom_country_list’); // That might not be the perfect hook to use
    function add_custom_country_list() {
    if (xprofile_get_field_id_from_name(‘Country’)) return;
    $country_list_args = array(
    ‘field_group_id’ => 1,
    ‘type’ => ‘selectbox’,
    ‘name’ => ‘Country’,
    ‘description’ => ‘Please select your country’,
    ‘is_required’ => true,
    ‘can_delete’ => false,
    ‘order_by’ => ‘default’
    );
    $country_list_id = xprofile_insert_field($country_list_args);
    if ($country_list_id) {
    $countries = array(‘USA’,’Germany’,’England’); // Google for “country list php” and replace this one
    foreach ($countries as $i => $country) {
    xprofile_insert_field(array(
    ‘field_group_id’ => 1,
    ‘parent_id’ => $country_list_id,
    ‘type’ => ‘selectbox’, // it is ‘selectbox’ not ‘option’
    ‘name’ => $country,
    ‘option_order’ => $i+1
    ));
    }
    }
    }
    `

    @webheadllc

    Participant

    This code worked except my options weren’t ordered correctly. The option_order arg didn’t work. It looks like option_order does not get saved in the database. Instead I used the following code where it uses whatever order the country array is in:

    `function your_plugin_activation() {
    //country
    wh_add_country_list();
    }
    //on activation or if you add this to functions use add_action ‘bp_init’
    register_activation_hook(__FILE__, ‘your_plugin_activation’);

    function wh_add_country_list() {
    if (xprofile_get_field_id_from_name(‘Country’)) return;

    add_filter(‘xprofile_field_options_before_save’, ‘wh_countries’);
    add_filter(‘xprofile_field_default_before_save’, ‘wh_country_option_default’);
    $country_list_args = array(
    ‘field_group_id’ => 1,
    ‘type’ => ‘selectbox’,
    ‘name’ => ‘Country’,
    ‘description’ => ‘Please select your country’,
    ‘is_required’ => true,
    ‘can_delete’ => false,
    ‘order_by’ => ‘default’
    );
    $country_list_id = xprofile_insert_field($country_list_args);
    remove_filter(‘xprofile_field_options_before_save’, ‘wh_countries’);
    remove_filter(‘xprofile_field_default_before_save’, ‘wh_country_option_default’);
    }

    function wh_country_option_default() {
    return array(‘US’ => 1);
    }

    function wh_countries() {
    return array(
    ‘US’ => ‘United States’
    //list of other countries
    );
    }`

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create custom field in ’base’ group manually’ is closed to new replies.
Skip to toolbar