While custom code is not required to add fields, we do offer support for creating or extending fields via custom code. If you aren’t comfortable with coding, use the settings page. Our most popular user field types can be added via a single settings page in the WordPress admin.
The code example below demonstrates how to use the PMPro User Fields API to register new fields, including advanced field types not offered via the settings page. You can also use custom code to extend the features of a field that was added via the settings page.
Creating Fields: Using the PMPro_Field
Class
To create fields via custom code you must do two things:
- Create a new
PMPro_Field
object with an array of properties and options - Add the field using the
function.pmpro_add_user_field
The PMPro_Field
class has various options for different types of fields, but there is a set of general options which applies to every type. View all supported field types and attributes »
// Create a new text field for Company. $fields[] = new PMPro_Field( 'company', 'text', array( 'label' => 'Company', 'size' => 40, 'class' => 'company', 'profile' => true, 'required' => true, 'levels' => array(1,2), 'memberslistcsv' => true ) );
- The
name
attribute. This is the meta key that is used when storing the field value in thewp_usermeta
table. The name must only contain letters, numbers, and underscores (no spaces, dashes or special characters). - The field
type
. See all field types here. - An
array
of options. There are some options that apply to every field type, and some that are unique to specific field types. See all option values here.
Deprecated Method to Add Fields using Register Helper
In the former method to add fields using the Register Helper Add On (now merged into core PMPro), this class was named
. This class and the methods to create fields is replaced with the PMProRH_Field
()PMPro_Field
class, which accepts the same parameters to build custom fields.
// Deprecated: Create a new Register Helper text field for Company. $fields[] = new PMProRH_Field( 'company', 'text', array( 'label' => 'Company', 'size' => 40, 'class' => 'company', 'profile' => true, 'required' => true, 'levels' => array(1,2), 'memberslistcsv' => true ) );
Creating Field Groups: Using the pmpro_add_field_group
Function
You can add additional locations for your fields called “field groups”.
- Each group is displayed in its own section of the Membership Checkout page or User Profile page
- Field groups have a name, label, and description.
- By default, new field groups are displayed in the order they are added. If you do not specify a field group label, the default label is “More Information”.
// Add a field group to put our fields into. pmpro_add_field_group( 'Business Details', 'Your Billing Details', 'Enter your billing details below.' );
To add a new field group, use the pmpro_add_field_group
function. This function accepts 4 parameters:
- the ID of the field group
- (optional) the label for the field group (Default: “More Information”)
- (optional) the description for the field group
- (optional) the order of the field group
Deprecated Method to Add ‘Checkout Boxes’ using Register Helper
In the former method to add fields using the Register Helper Add On (now merged into core PMPro), this function was named pmprorh_add_checkout_box()
. This function is replaced with the pmpro_add_field_group
function, which accepts the same parameters in the same order.
// Deprecated: Add a checkout_box to put Register Helper fields into. pmprorh_add_checkout_box( 'billing_details', 'Billing Details', 'Enter your billing details below.' );
Action and Filter Hooks
- pmpro_default_field_group_label: Modify the default label for a new field group when creating groups on the Memberships > Settings > User Fields page
- pmpro_add_user_field_where: Filter the group to add the field to.
- pmpro_add_user_field: Filter the field to add.
- pmpro_user_taxonomy_args: Filter to modify the args passed to the custom user taxonomy registered by custom User Fields code.