xprofile_set_field_data

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

Description

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

Parameters (4)

0. $field (int|string)
The ID of the field, or the $name of the field.
1. $user_id (int)
The ID of the user.
2. $value (mixed)
The value for the field you want to set for the user.
3. $is_required — Optional. (bool) => false
Whether or not the field is required.

Usage

  1. if ( !function_exists( 'xprofile_set_field_data' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-xprofile/bp-xprofile-functions.php'; 
  3.  
  4. // The ID of the field, or the $name of the field. 
  5. $field = null; 
  6.  
  7. // The ID of the user. 
  8. $user_id = -1; 
  9.  
  10. // The value for the field you want to set for the user. 
  11. $value = null; 
  12.  
  13. // Whether or not the field is required. 
  14. $is_required = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = xprofile_set_field_data($field, $user_id, $value, $is_required); 
  18.  

Defined (1)

The function is defined in the following location(s).

/bp-xprofile/bp-xprofile-functions.php  
  1. function xprofile_set_field_data( $field, $user_id, $value, $is_required = false ) { 
  2.  
  3. if ( is_numeric( $field ) ) { 
  4. $field_id = $field; 
  5. } else { 
  6. $field_id = xprofile_get_field_id_from_name( $field ); 
  7.  
  8. if ( empty( $field_id ) ) { 
  9. return false; 
  10.  
  11. $field = xprofile_get_field( $field_id ); 
  12. $field_type = BP_XProfile_Field::get_type( $field_id ); 
  13. $field_type_obj = bp_xprofile_create_field_type( $field_type ); 
  14.  
  15. /** 
  16. * Filter the raw submitted profile field value. 
  17. * Use this filter to modify the values submitted by users before 
  18. * doing field-type-specific validation. 
  19. * @since 2.1.0 
  20. * @param mixed $value Value passed to xprofile_set_field_data(). 
  21. * @param BP_XProfile_Field $field Field object. 
  22. * @param BP_XProfile_Field_Type $field_type_obj Field type object. 
  23. */ 
  24. $value = apply_filters( 'bp_xprofile_set_field_data_pre_validate', $value, $field, $field_type_obj ); 
  25.  
  26. // Special-case support for integer 0 for the number field type. 
  27. if ( $is_required && ! is_integer( $value ) && $value !== '0' && ( empty( $value ) || ! is_array( $value ) && ! strlen( trim( $value ) ) ) ) { 
  28. return false; 
  29.  
  30. /** 
  31. * Certain types of fields (checkboxes, multiselects) may come through empty. 
  32. * Save as empty array so this isn't overwritten by the default on next edit. 
  33. * Special-case support for integer 0 for the number field type 
  34. */ 
  35. if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && $field_type_obj->accepts_null_value ) { 
  36. $value = array(); 
  37.  
  38. // If the value is empty, then delete any field data that exists, unless the field is of a type 
  39. // where null values are semantically meaningful. 
  40. if ( empty( $value ) && ! is_integer( $value ) && $value !== '0' && ! $field_type_obj->accepts_null_value ) { 
  41. xprofile_delete_field_data( $field_id, $user_id ); 
  42. return true; 
  43.  
  44. // For certain fields, only certain parameters are acceptable, so add them to the whitelist. 
  45. if ( $field_type_obj->supports_options ) { 
  46. $field_type_obj->set_whitelist_values( wp_list_pluck( $field->get_children(), 'name' ) ); 
  47.  
  48. // Check the value is in an accepted format for this form field. 
  49. if ( ! $field_type_obj->is_valid( $value ) ) { 
  50. return false; 
  51.  
  52. $field = new BP_XProfile_ProfileData(); 
  53. $field->field_id = $field_id; 
  54. $field->user_id = $user_id; 
  55. $field->value = maybe_serialize( $value ); 
  56.  
  57. return $field->save();