Custom user data fields with WordPress Membership plugin

Here’s a quick overview of integrating custom user fields with Membership plugin (v2.0.7 at the time of writing).

Basically, we’ll have to:

  • Create some fields to be added after Membership’s own registration form
  • Create a function to validate and save the new registration form fields on submission
  • Copy-paste the whole user account edit form from Membership plugin and modify it to suit our needs
  • Create a function to override Membership’s own account edit form

Adding custom fields to the registration form
To do that, let’s utilize Membership’s action
membership_subscription_form_registration_presubmit_content
to load our own template from the theme directory – let’s call it
namespace_account_register_form.php

// functions.php

function namespace_add_custom_registration_fields() {
 require_once( get_stylesheet_directory() . "/namespace_account_register_form.php" );
}
add_action( 'membership_subscription_form_registration_presubmit_content', 'namespace_add_custom_registration_fields');

Basically, this will pull the contents of the form template and display them after Membership’s own registration form.

The form itself should be obvious. To save space, I’ll give an example with just one field.

<!-- /wp-content/themes/[active theme]/namespace_account_register_form.php -->

<label for="user_pokemon"><?php _e('Favorite Pokémon','membership'); ?> <span>*</span></label>
<input type="text" value="<?php echo esc_attr($_POST['user_pokemon']); ?>" class="regtext" name="user_pokemon" id="user_pokemon">

Saving the submitted registration form 
When membership_subscription_form_registration_process
is fired, just validate and save the usermeta as you normally would

// functions.php

function namespace_add_custom_registration_fields_process($errors, $user_id) {
if ( empty($errors) ) {
    if (isset($_POST['user_pokemon'])) {
      $usermeta['user_pokemon'] = $_POST['user_pokemon'];
    }
    update_user_meta($user_id, 'namespace_userdata', $usermeta);
  }
}
add_action( 'membership_subscription_form_registration_process', 'namespace_add_custom_registration_fields_process', 10, 2);

New edit user form 
Unfortunately Membership appears to have just one filter we can use for this,
membership_override_account_form,
so the easiest solution here is to just copy-paste the whole form from
/wp-content/plugins/membership/membershipincludes/includes/account.form.php
and modify it to suit your needs.

This will simply override the default Membership form with our own.

// functions.php

function namespace_override_account_form() {
 return get_stylesheet_directory() . '/namespace_account_edit_form.php';
}
add_filter( 'membership_override_account_form', 'namespace_override_account_form' );

As for modifying the form – for example, you can take the if statement from line 29 which checks for errors, and add the functionality for saving usermeta to the else block:

// /wp-content/themes/[active theme]/namespace_account_edit_form.php

if ( isset( $errors ) && is_wp_error( $errors ) ) {
 $msg = implode( "</p>\n<p>", $errors->get_error_messages() );
} else {
  // If no errors, update usermeta with custom fields
  $usermeta = array();
  if (isset($_POST['user_pokemon'])) {
    $usermeta['user_pokemon'] = $_POST['user_pokemon'];
  }
  update_user_meta($user_id, 'namespace_userdata', $usermeta);
}

And add the new field to the form in the same file, below:

// Get metadata for pre-populating the forms
$namespace_userdata = get_the_author_meta( 'namespace_userdata', $user_id ); 

// The new form field should look something like this
<tr style='background: transparent;'>
  <th><label for="user_pokemon"><?php _e('Favorite Pokémon', 'membership'); ?></label></th>
  <td>
    <input type="text" name="user_pokemon" id="user_pokemon" value="<?php echo esc_attr( $namespace_userdata['user_pokemon'] ); ?>" class="regular-text" />
  </td>
</tr>

And that’s it. Don’t forget to escape the output properly!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>