RELATED TO: RealHomes Theme

From RealHomes version 3.9.5, you can add any number of text and select/dropdown, New User Fields using inspiry_additional_user_fields hook.

Field Settings:

id
Field id. Required and must be unique. It will be used as meta_key when saving to the database. It’s a good practice to use only numbers, letters and underscores.
name
Field label. It’s required to display a field.
title
Field title. Optional but recommended to add more detail about a field.
type
Field type. select or text (default)
options
Field options are required to display a select/dropdown field.
show
Field show. Required to display a field on different forms. There are a total of three Register, Frontend Profile, and Backend Profile forms that you can choose to display the New User Fields.
required
Require field in the form. true or false (default)

 

Sample Code

function inspiry_add_additional_user_fields() {

	$form_fields = array(
		array(
			'name'     => esc_html__( 'Sample Text Field', 'framework' ),
			'id'       => 'sample_text_field',
			'type'     => 'text', // optional
			'title'    => esc_html__( '* Sample user text field.' ),
			'show'     => array( 'register_form', 'profile_backend', 'profile_frontend' ), // display on All forms
			'required' => true
		),
		array(
			'name'     => esc_html__( 'Sample Dropdown Field', 'framework' ),
			'id'       => 'sample_dropdown_field',
			'type'     => 'select',
			'options'  => array(
				''           => 'Select an option',
				'1st_option' => '1st Option',
				'2nd_option' => '2nd Option',
				'3rd_option' => '3rd Option',
			),
			'title'    => esc_html__( '* Sample user dropdown field.' ),
			'show'     => array( 'register_form', 'profile_frontend' ), // display only on Register and Frontend Profile forms
			'required' => true
		),
		array(
			'title' => esc_html__( 'Sample Checkbox Field Title', 'framework' ), // frontend
			'name'  => esc_html__( 'Sample Checkbox Field Name', 'framework' ), // backend
			'id'    => 'sample_checkbox_field', // must be unique
			'type'  => 'checkbox',
			'std'   => 'yes', // must be unique
			'show'  => array( 'register_form', 'profile_backend', 'profile_frontend' ), // display on All forms
		),
	);

	return $form_fields;
}

add_filter( 'inspiry_additional_user_fields', 'inspiry_add_additional_user_fields' );

 

Method of Use:
Copy the above sample code to your RealHomes Child Theme’s functions.php file and modify/add User Fields according to your needs.

Retrieving Fields Value:
Values of additional user fields that we just added can be displayed using the following code:

$user_id = get_current_user_id(); // getting the current logged in user id

$text_field_value     = get_user_meta( $user_id, 'sample_text_field', true ); // getting field value against 'sample_text_field' key
$dropdown_field_value = get_user_meta( $user_id, 'sample_dropdown_field', true ); // getting field value against 'sample_dropdown_field' key

echo $text_field_value; // displaying first field value
echo $dropdown_field_value; // displaying second field value