User Fields is a robust feature in Paid Memberships Pro to create custom profile fields for your membership site. This post covers a lesser known feature of that Add On: the field’s custom save_function. You can use a custom callback function for any field to modify how and where the field’s value is saved.

Banner Image for How to Use a Custom Callback Function For Use Profile Fields

Saving User Fields With a Custom Callback Function

Every user field can be extended with a custom save_function. This callback function opens up a whole range of possibilities that you or your developer may incorporate to extend how a field data is processed, such as:

  • Format field input data before saving to the database.
  • Perform server-side data verification such as validate a member’s location, age, qualifications, and more.
  • Save the field input to any database table in your WordPress site, not just the default location in usermeta.

Using the save_function Custom Callback

Custom callback functions for a field require that you do two things:

  1. Write the custom function.
  2. Add the custom function to the field array in your custom user fields code. The save_function field attribute accepts a string value of the callback function’s name.
'save_function' => 'my_custom_callback_function'

The save function is attached to a specific user field you create either with the User Fields Settings page or via custom code. Here are two examples of adding a save_function to a user field.

Recipe #1: Create User Field and Save Function in Custom Code

Here is a basic example of setting a callback function with the save_function option on line 17.

<?php
// Create a registration form field with a custom callback
function my_pmpro_user_field_example_save_function() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
// define the fields
$fields = array();
// Basic Text Field Example
$fields[] = new PMPro_Field(
'my_field_name', // input field name, used as meta key
'text', // field type
array(
'label' => 'My Field Label', // field label
'profile' => true, // display on user profile
'save_function' => 'my_custom_callback_function', // use a custom callback function
)
);
foreach ( $fields as $field ) {
pmpro_add_user_field(
'checkout_boxes', // location on checkout page
$field // PMPro_Field object
);
}
}
add_action( 'init', 'my_pmpro_user_field_example_save_function' );
// Custom callback function
function my_custom_callback_function( $field_value ) {
// Do something
}

Recipe #2: Filter a User Field Created in Settings to Add a Save Function

For this example, the user field would have already been created on the Memberships > Settings > User Fields screen in the WordPress admin.

This code extends that field to add your custom save_function. Update the field_name on line 19 for your user field name.

<?php
/**
* Example of adding a save function custom callback function to a field
* using the "pmpro_add_user_field" filter hook.
*
* title: Example add custom callback save function for a user field.
* layout: snippet-example
* collection: user-fields
* category: custom-fields
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_user_field_add_save_function_example( $field, $where ) {
// Match our field name
if ( 'field_name' === $field->name ) {
// Add save function for our field.
$field->save_function = 'custom_callback_function_name'; // Set to your callback function name.
}
return $field;
}
add_filter( 'pmpro_add_user_field', 'my_pmpro_user_field_add_save_function_example', 10, 2 );
// Custom callback function
function custom_callback_function_name( $field_value ) {
// Do something
}

Save Function Example: Capitalize the Input Value Before Saving

In this practical example, we add a custom field to collect “Pet Name” for our Must Love Dogs demo site. Since name is a proper noun, we want to make sure our pet’s name is properly capitalized before saving it to the database. Here are the steps:

  1. Create a text input field that collects the pet’s name.
  2. Create a custom function that capitalizes each word in a string.
  3. Add the save_function option to our field calling our custom function to process the field handling.
<?php
// Create a registration form field with a custom callback
function my_pmpro_user_field_example_save_function() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
// define the fields
$fields = array();
// Basic Text Field Example
$fields[] = new PMPro_Field(
'pet_name', // input field name, used as meta key
'text', // field type
array(
'label' => 'Pet Name', // field label
'profile' => true, // display on user profile
'save_function' => 'my_capitalize_registration_field_value', // use a custom callback function
)
);
foreach ( $fields as $field ) {
pmpro_add_user_field(
'checkout_boxes', // location on checkout page
$field // PMPro_Field object
);
}
}
add_action( 'init', 'my_pmpro_user_field_example_save_function' );
// Custom callback function to capitalize string value
function my_capitalize_registration_field_value( $user_id, $field_name, $field_value ) {
// Do nothing if no value or if not string
if ( empty( $field_value ) || ! is_string( $field_value ) ) {
return;
}
// Captilize words
$field_value = ucwords( $field_value );
// Sanitize before saving to the database.
$field_value = sanitize_text_field( $field_value );
// Save to the usermeta table
update_user_meta( $user_id, $field_name, $field_value );
}

Here’s an example of using this approach to create a custom save function to store more information for a Digital Signature-type field.

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

Writing the exact custom function to save your fields requires advanced WordPress and PHP development experience. This example is a fairly simple application of the power of a custom callback function but demonstrates the range of possibilities for how form inputs are handled.

I have personally found this custom callback to be the PMPro User Fields API superhero secret power, and I hope you find as much joy as I do from it. Remember to always sanitize your data before saving it to the database.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics


Was this article helpful?
YesNo