Description
This filter allows you to make changes to the fields settings array before the fields are passed to the form building functions. This allows you to add/subtract fields from different forms and/or on different pages.
For example, you may wish to display fields on the user profile update that are not included in registration. This function allows you to make those changes in one place so that you do not have to worry about filtering the fields out of the form and then also filtering them for processing. Both are handled by this filter.
Parameters
$fields
(array) (required) The form fields settings array.
$tag
(string) (optional) Indicates the process for the form (register|profile|register_wp|profile_dashboard|profile_admin).
Examples
Remove default address/phone fields from the WP-Members registration form, but not the user profile update form:
/** * This example removes the WP-Members default address/phone fields * from the form if it is being displayed for registration, but * would leave them in for user profile update or any other * process. */ add_filter( 'wpmem_fields', 'my_remove_reg_fields_filter', 10, 2 ); function my_remove_reg_fields_filter( $fields, $tag ) { if ( 'register' == $tag ) { unset( $fields['billing_address_1'] ); unset( $fields['billing_address_2'] ); unset( $fields['billing_city'] ); unset( $fields['billing_state'] ); unset( $fields['billing_postcode'] ); unset( $fields['billing_country'] ); unset( $fields['billing_phone'] ); } return $fields; }
Similar to the above example, but uses an array of meta keys, removing from the registration form any field whose meta key is in the array.
/** * Remove fields from the registration form using and array of * meta keys. Loops through the array to remove (unset) fields * found in the array from the settings. */ add_filter( 'wpmem_fields', 'my_remove_reg_fields_by_array', 10, 2 ); function my_remove_reg_fields_by_array( $fields, $tag ) { if ( 'register' == $tag ) { $remove_meta = array ( 'billing_address_1', 'billing_address_2', 'billing_postcode', 'billing_country' ); foreach ( $remove_meta as $meta ) { unset( $fields[ $meta ] ); } } return $fields; }
An example of removing a field from the user profile update form but not the registration:
/** * This example removes a field (in this example - phone1) from * the user profile update. That way, the user cannot update * or change this field after registration. */ add_filter( 'wpmem_fields', function ( $fields, $tag ) { if ( 'profile' == $tag || 'profile_dashboard' == $tag ) { unset( $fields['phone1'] ); } return $fields; }, 10, 2 ); /** * Same as above for several field meta keys in an array. */ add_filter( 'wpmem_fields', function ( $fields, $tag ) { // Only remove if we're on the profile or profile_dashboard view. if ( 'profile' == $tag || 'profile_dashboard' == $tag ) { // An array of field meta keys to remove. $remove_fields = array( 'my_field1', 'my_field2', 'my_field3' ); // Loop through the array and unset the key from the $fields array. foreach ( $remove_fields as $remove_field ) { unset( $fields[ $remove_field ] ); } } return $fields; }, 10, 2 );
Notes
The array key for each field in the array is its meta key. Each field in the array is an array of setting values as noted below.
All fields must include the following:
- label (string) The text for the field’s label.
- name (string) The field’s meta key. This is what will be stored in the database (wp_usermeta for non-native fields). Avoid spaces and hyphens (recommended: all lowercase characters, numbers, and underscores only).
- type (string) The field type:
- text
- select
- multiselect
- checkbox
- multicheckbox
- radio
- textarea
- url
- number
- date
- password
- file
- image (a “file” input type, but handles images specifically)
- hidden
- option (subset of “select”)
- membership
- register (bool|int) 1 if included in registration form, 0 if admin only
- required (bool|int) 1 if required
- profile (bool|int) For future development
- native (bool|int) 1 if a native WP field
The following values are supported by all fields, but are optional (the above are required)
- id (string) (optional) The “id” attribute of the HTML5 tag. Defaults to the field meta key if not specified.
- class (string) A custom CSS class name (defaults to “textbox” if no class is given)
The following values are supported by text, url, email, and password field types.
- placeholder (string) Placeholder text for an empty field
- title (string)
- pattern (string) A regular expression (regex) pattern as supported by HTML5
The following values are supported by the number field type:
- placeholder (string) Placeholder text for an empty field
- title (string)
- min
- max
The following values are supported by the date field type:
- placeholder (string) Placeholder text for an empty field
- title (string)
- pattern (string) A regular expression (regex) pattern as supported by HTML5
- min
- max
The following values are supported by the checkbox field type:
- checked_value (string) The value to save if the checkbox is checked.
- checked_default (bool|int) If the checkbox should be loaded as checked by default (1|true)
- checkbox_label (int) Defines if the checkbox label is added before or after the HTML input for the checkbox. 1 loads after (to the right). Defaults to 0 (loads label first/to the right).
Changelog
Introduced in version 3.1.7
Source
wpmem_fields is located in includes/api/api-forms.php