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.

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.

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.

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?