Adding Fields To The Free Subscription Sign-Up

Whether you use your free subscription to provide a step-up from anonymous access or to replace it completely, you’re more than likely want to collect more information about your subscriber than the standard sign-up fields of name and email.

Fortunately, Leaky Paywall provides a convenient hook that makes it easy to add fields to the free sign-up subscription form.

(If you are not overly familiar with actions and filters – “hooks” – then checkout this guide on the WordPress Codex.)

Let’s say we wanted to collect the organisation the subscriber works for. All we need to do is add the following code:

// add extra field to free registration form to capture user's organization
add_filter( 'leaky_paywall_after_registration_fields', 'extra_registration_fields' );

function extra_registration_fields( $fields ) {

 $output = '
 <p>
 <label for="organization">Organization</label>
 <input name="organization" id="organization" type="text"/>
 </p>
 ';

 return $fields . $output;
}


add_action( 'leaky_paywall_after_free_user_created', 'add_registration_fields_to_user', 10, 2 );
function add_registration_fields_to_user( $user_id, $posted ) {

 update_user_meta( $user_id, 'organization', $posted['organization'] );

}

Pretty straight forward. Now, you can just insert this code at the bottom of your theme’s functions.php and it will work just fine so long as you don’t change themes. WordPress purists would probably prefer that you created your own plugin as this is not really theme-related functionality but I’ll leave that for you to decide.

Once you’ve plugged the code in, your free subscription sign-up form will look something like this:

Extra field on free subscription sign-up
Our new field on the sign-up form

It’s important to note that you can’t make the field mandatory here – you’ll need to add client-side javascript to do that.

Also, you won’t immediately see the new field in the WordPress admin interface. It’s added as a custom user meta field, so it won’t appear in the subscriber table under Leaky Paywall > Subscribers. And custom user meta doesn’t appear by default on Edit User screen but, fortunately, is easy to add via the show_user_profile and edit_user_profile action hooks.

Go back to your theme’s functions.php file or your custom plugin and add the following code:

add_action( 'show_user_profile', 'display_extra_profile_fields' );
add_action( 'edit_user_profile', 'display_extra_profile_fields' );
function display_extra_profile_fields( $user ){ 
?> 
 <table class="form-table">
 <tr>
 <th><label for="organization">Organization</label></th>
 <td><input type="text" name="organization" value="<?php echo esc_attr(get_the_author_meta( 'organization', $user->ID )); ?>" class="regular-text" /></td>
 </tr>
 </table>
<?php
}

This code adds a new text input to the view profile screen (only the user can see this screen) and the edit profile screen (via Users in the admin interface) using the WordPress provided function get_the_author_meta to set its value.

Extra field on user profile
The extra field on the user profile

That’s great, but you probably want the field to be updatable as well. So, let’s also add this:

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

function save_extra_profile_fields( $user_id )
{
 update_user_meta( $user_id,'organization', sanitize_text_field( $_POST['organization'] ) );
}

This simply hooks into the profile update process and makes sure that organization is saved along with the rest of the user profile data.

Now, you have a new field on your free subscription sign-up form and it can be updated by the user or an admin. And, of course, you can add as many fields as you need but remember you don’t want to make that sign-up too much of a hurdle, even for a free subscription.

PLEASE NOTE: This article only applies to the sign-up for free subscriptions where the processing is completely managed by Leaky Paywall. This will not work for any subscriptions where payment processing is required.