xprofile_set_field_data( int|string $field, int $user_id, mixed $value, bool $is_required = false )

A simple function to set profile data for a specific field for a specific user.


Parameters Parameters

$field

(int|string) (Required) The ID of the field, or the $name of the field.

$user_id

(int) (Required) The ID of the user.

$value

(mixed) (Required) The value for the field you want to set for the user.

$is_required

(bool) (Optional) Whether or not the field is required.

Default value: false


Top ↑

Return Return

(bool) True on success, false on failure.


Source Source

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

function xprofile_set_field_data( $field, $user_id, $value, $is_required = false ) {

	if ( is_numeric( $field ) ) {
		$field_id = $field;
	} else {
		$field_id = xprofile_get_field_id_from_name( $field );
	}

	if ( empty( $field_id ) ) {
		return false;
	}

	$field          = xprofile_get_field( $field_id );
	$field_type     = BP_XProfile_Field::get_type( $field_id );
	$field_type_obj = bp_xprofile_create_field_type( $field_type );

	/**
	 * Filter the raw submitted profile field value.
	 *
	 * Use this filter to modify the values submitted by users before
	 * doing field-type-specific validation.
	 *
	 * @since 2.1.0
	 *
	 * @param mixed                  $value          Value passed to xprofile_set_field_data().
	 * @param BP_XProfile_Field      $field          Field object.
	 * @param BP_XProfile_Field_Type $field_type_obj Field type object.
	 */
	$value = apply_filters( 'bp_xprofile_set_field_data_pre_validate', $value, $field, $field_type_obj );

	// Special-case support for integer 0 for the number field type.
	if ( $is_required && ! is_integer( $value ) && $value !== '0' && ( empty( $value ) || ! is_array( $value ) && ! strlen( trim( $value ) ) ) ) {
		return false;
	}

	/**
	 * Certain types of fields (checkboxes, multiselects) may come through empty.
	 * Save as empty array so this isn't overwritten by the default on next edit.
	 *
	 * Special-case support for integer 0 for the number field type
	 */
	if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && $field_type_obj->accepts_null_value ) {
		$value = array();
	}

	// If the value is empty, then delete any field data that exists, unless the field is of a type
	// where null values are semantically meaningful.
	if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && ! $field_type_obj->accepts_null_value ) {
		xprofile_delete_field_data( $field_id, $user_id );
		return true;
	}

	// For certain fields, only certain parameters are acceptable, so add them to the list of allowed values.
	if ( $field_type_obj->supports_options ) {
		$field_type_obj->set_allowed_values( wp_list_pluck( $field->get_children(), 'name' ) );
	}

	// Check the value is in an accepted format for this form field.
	if ( ! $field_type_obj->is_valid( $value ) ) {
		return false;
	}

	$field_args = compact( 'field_type_obj', 'field', 'user_id', 'value', 'is_required' );

	/**
	 * Return a WP_Error object or true to use your custom way of saving field values.
	 *
	 * @since 8.0.0
	 *
	 * @param boolean Whether to shortcircuit the $bp->profile->table_name_data table.
	 * @param array $field_args {
	 *     An array of arguments.
	 *
	 *     @type object            $field_type_obj Field type object.
	 *     @type BP_XProfile_Field $field          Field object.
	 *     @type integer           $user_id        The user ID.
	 *     @type mixed             $value          Value passed to xprofile_set_field_data().
	 *     @type boolean           $is_required    Whether or not the field is required.
	 * }
	 */
	$retval = apply_filters( 'bp_xprofile_set_field_data_pre_save', false, $field_args );

	if ( is_wp_error( $retval ) ) {
		return false;
	}

	if ( false === $retval ) {
		$field           = new BP_XProfile_ProfileData();
		$field->field_id = $field_id;
		$field->user_id  = $user_id;

		// Gets un/reserialized via xprofile_sanitize_data_value_before_save().
		$field->value    = maybe_serialize( $value );

		$retval = $field->save();
	}

	return $retval;
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.