xprofile_insert_field( array|string $args = '' )

Insert or update an xprofile field.

Description

Parameters

$args

(array|string) (Optional) Array of arguments.

  • 'field_id'
    (int) Optional. Pass the ID of an existing field to edit that field.
  • 'field_group_id'
    (int) ID of the associated field group.
  • 'parent_id'
    (int) Optional. ID of the parent field.
  • 'type'
    (string) Field type. Checked against a field_types whitelist.
  • 'name'
    (string) Name of the new field.
  • 'description'
    (string) Optional. Descriptive text for the field.
  • 'is_required'
    (bool) Optional. Whether users must provide a value for the field. Default: false.
  • 'can_delete'
    (bool) Optional. Whether admins can delete this field in the Dashboard interface. Generally this is false only for the Name field, which is required throughout BP. Default: true.
  • 'order_by'
    (string) Optional. For field types that support options (such as 'radio'), this flag determines whether the sort order of the options will be 'default' (order created) or 'custom'.
  • 'is_default_option'
    (bool) Optional. For the 'option' field type, setting this value to true means that it'll be the default value for the parent field when the user has not yet overridden. Default: true.
  • 'option_order'
    (int) Optional. For the 'option' field type, this determines the order in which the options appear.

Default value: ''

Return

(bool|int) False on failure, ID of new field on success.

Source

File: bp-xprofile/bp-xprofile-functions.php

function xprofile_insert_field( $args = '' ) {

	$r = wp_parse_args( $args, array(
		'field_id'          => null,
		'field_group_id'    => null,
		'parent_id'         => null,
		'type'              => '',
		'name'              => '',
		'description'       => '',
		'is_required'       => false,
		'can_delete'        => true,
		'order_by'          => '',
		'is_default_option' => false,
		'option_order'      => null,
		'field_order'       => null,
	) );

	// Field_group_id is required.
	if ( empty( $r['field_group_id'] ) ) {
		return false;
	}

	// Check this is a non-empty, valid field type.
	if ( ! in_array( $r['type'], (array) buddypress()->profile->field_types ) ) {
		return false;
	}

	// Instantiate a new field object.
	if ( ! empty( $r['field_id'] ) ) {
		$field = xprofile_get_field( $r['field_id'] );
	} else {
		$field = new BP_XProfile_Field;
	}

	$field->group_id = $r['field_group_id'];
	$field->type     = $r['type'];

	// The 'name' field cannot be empty.
	if ( ! empty( $r['name'] ) ) {
		$field->name = $r['name'];
	}

	$field->description       = $r['description'];
	$field->order_by          = $r['order_by'];
	$field->parent_id         = (int) $r['parent_id'];
	$field->field_order       = (int) $r['field_order'];
	$field->option_order      = (int) $r['option_order'];
	$field->is_required       = (bool) $r['is_required'];
	$field->can_delete        = (bool) $r['can_delete'];
	$field->is_default_option = (bool) $r['is_default_option'];

	return $field->save();
}

Changelog

Changelog
Version Description
BuddyPress 1.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.