Do you want to add some custom user fields to your membership site but don’t know how? In this step-by-step guide, we show you how to code custom User Fields in PMPro (formerly know as Register Helper).

Step by Step Guide to Creating Custom Fields Using Code Banner Image

What are User Fields?

User Fields is a robust feature in Paid Memberships Pro to create custom profile fields for your membership site. User fields helps you set up custom checkout fields by level, collect specific information about your members, and add this collected data to your member’s profile. Fields can be collected at membership checkout, on the user’s profile or for administrative view-only.

You can do a whole lot with User Fields. And when we merged Register Helper into the core Paid Memberships Pro plugin, we wanted give non-developers a way to add fields via a GUI (graphical user interface), without limiting how developers can extend the fields via the User Fields API.

  • You can set up text-based custom fields, such as collect telephone numbers
  • You can add a dropdown field to your checkout page.
  • You can add fields that only the admin can see and populate. You can even add fields that users can view, but only admins can edit.
  • You can create a field that depends on another field’s value.
  • You can save field data in other places, not just the user object.
  • The options are (almost) endless.

Warning: we are about to get into some developer-focused stuff. If you are not comfortable with code—I encourage you to keep reading. The basic principles of setting up a custom field with code are actually pretty simple to grasp. You can do it.

But please know that you have no-code options as well:

Let’s go!

Video Demo

Before We Get Started

Before we dive into any coding, we will first need a couple of things in place:

  1. Install and activate Paid Memberships Pro on your website.
  2. Download, install, and activate the Code Snippets plugin. We will be adding our code to this 3rd party plugin.

Note – We recommend trying this first on a test or staging environment. This will allow you to first test and practice this method before trying it out on your live production site. You can use Local by Flywheel to set up a free staging site.

Using Custom Code to Add User Fields

Once you have installed and activated Paid Memberships Pro and the Code Snippets plugin. You are ready to start coding up your custom fields.

WordPress Dashboard screenshot. Displaying Paid Memberships Pro, Register Helper, and the Code Snippets plugin.

Prepare Your Code Snippet

First, let’s go ahead and configure the Code Snippets plugin:

  1. Open up the Code Snippets plugin by selecting “Snippets” > “Add New” from the WP Dashboard.
  2. Name your code snippet. I named mine “Custom Checkout Fields”.
  3. Give your code snippet a description and (if applicable) add any tags to your code snippet.
  4. Set your code snippet to “Run snippet everywhere”. If needed, you can set your code snippet priority.
Screenshot of the Code Snippets > Add New Snippet screen in the WordPress admin.
Screenshot of the Code Snippets > Add New Snippet screen in the WordPress admin.

Starting a Function

  1. First, create a function that runs on init, a WordPress action hook otherwise known as Initialize. This action hook ‘fires’ after WordPress has finished loading but before any headers are sent. Learn more about the init action hook here.
function my_pmpro_add_fields_to_checkout() {
   // This is where our fields code will go.
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );

You can name your function however you would like. For this example, we are using the function name my_pmpro_add_fields_to_checkout.

  1. Next, add an IF Statement to your function, that checks to see if the User Fields functionality in Paid Memberships Pro is activate. If this function is not active, return a boolean value of false. This step prevent any errors coming up if for some reason Register Helper gets deactivated or is no longer located on your site.
function my_pmpro_add_fields_to_checkout() {
	// Don't break if PMPro is out of date or not loaded.
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// This is where our fields code will go.
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );
  1. Create an empty fields array, $fields = array();. This is where we will create our custom fields. This is currently empty and won’t display anything if saved.
function my_pmpro_add_fields_to_checkout() {
	// Don't break if PMPro is out of date or not loaded.
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// This is where our fields code will go.
	$fields = array();

}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );
  1. Next, create a field group. This code defines a new “box” with a heading and optional description wherever your new fields are displayed.
function my_pmpro_add_fields_to_checkout() {
	// Don't break if PMPro is out of date or not loaded.
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// This is where our fields code will go.
	$fields = array();

	// Add a field group to put our fields into.
	pmpro_add_field_group( 'Business Details' );
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );
  1. Finally, add a foreach loop. This loop checks each of the custom fields (not added yet) and places them in our field group.
function my_pmpro_add_fields_to_checkout() {
	// Don't break if PMPro is out of date or not loaded.
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// This is where our fields code will go.
	$fields = array();

	// Add a field group to put our fields into.
	pmpro_add_field_group( 'Business Details' );

	// Add all of our fields into that group.
	foreach ( $fields as $field ) {
		pmpro_add_user_field(
			'Business Details',
			$field
		);
	}
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );

You should have something very similar to the code above. If not, feel free to copy the code block above and add it to your Code Snippets plugin.

Nothing should happen if the code is run. This is an empty function that is ready and waiting for fields to be added to it.

Add User Fields to Our Function

It is time to start adding the actual custom fields to our code. In your function, locate the $fields = array();. The next portion of this tutorial adds code right under this section.

In this tutorial, I am going to make a “Company” field for members to input their Company’s name when performing a checkout. We need to create a new array item in the $fields array variable and assign it three main parameters:

  1. Name – Assign the input name of the field here. This will also be used as the designated field meta key. In this case, we will use name “company”.
  2. Type of field – Select the type of field here. There are multiple field types that you can choose from. We want members to write out their company name so we will use a “text” field.
  3. An array of options – There are some options that apply to every field type, and some that are unique to specific field types. I will add three attributes to this array. Learn more about the types of array options available here.
    • Label  – This label will be added to the custom field on the frontend of the PMPro checkout page. I am using “Company” for this example.
    • Profile – Display or hide this field from the member’s profile. I have set this to true.
    • Required –  This will make the field required. I have this set to true.
$fields[] = new PMPro_Field(
	'company',
	'text',
	array(
		'label' => 'Company',
		'size' => 40,
		'class' => 'company',
		'profile' => true,
		'required' => true,
		'levels' => array(1,2),
		'memberslistcsv' => true
        )
);
Screenshot of a checkout page with the Company text input user field.
Screenshot of a checkout page with the Company text input user field.

If your code snippet is active, you should see the ‘Business Details’ section and the ‘Company’ field on your membership site’s checkout page. If not, copy the code below and add it to your Code Snippet plugin.

function my_pmpro_add_fields_to_checkout() {
	// Don't break if PMPro is out of date or not loaded.
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// This is where our fields code will go.
	$fields = array();

	// Add a Company text field.
	$fields[] = new PMPro_Field(
		'company',
		'text',
		array(
			'label' => 'Company',
			'size' => 40,
			'class' => 'company',
			'profile' => true,
			'required' => true,
			'levels' => array(1,2),
			'memberslistcsv' => true
	        )
	);

	// Add a field group to put our fields into.
	pmpro_add_field_group( 'Business Details' );

	// Add all of our fields into that group.
	foreach ( $fields as $field ) {
		pmpro_add_user_field(
			'Business Details',
			$field
		);
	}
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );

Adding More Fields

At this point, you have a working example of how to add user fields via code.

  • If you want to add more fields, copy the entire $fields[] = new PMPro_Field section of code.
  • Then, paste the code directly under the previous field.
  • Once done, you should have two of the same fields.
  • Now edit the second field by changing the field parameters and attributes.
Example of a membership checkout page with multiple user fields
Example of a membership checkout page with multiple user fields

Custom Coded Fields Wrap Up

If you have followed along with this tutorial, you should have a Paid Memberships Pro checkout page with a “Company” custom field in a box titled “Business Details”.

If this User Fields API is new to you, know that the more you become familiar with the documentation, the better you will become at adding custom fields to Paid Memberships Pro.

Feel free to use the code example in this guide as a starter for your custom fields.

  1. Copy and paste the code from this example into your Code Snippets plugin.
  2. Change a few lines of code to match what fields you’re looking to create
  3. Activate the snippet.

Continue learning about user fields in Paid Memberships Pro in our documentation:

Cover image from ebook 29 Nuggets of Wisdom Volume 1 - Sample Collection

Download the free ebook: Get 29 insights and ‘aha moments’ for new or veteran membership site business owners. Use these nuggets of wisdom to inspire or challenge you.

Was this article helpful?
YesNo