Sep 192014

I have a requirement to build a WordPress sites for an organisation that wants to charge members to belong to the organisation. I looked at the membership plugins that are available currently and they are all geared to making members pay for access to content rather than for the simpler needs of a club or organisation that wants some of their website pages available to non-members and the remainder of the site only available to members. As a result of nothing being available off the shelf as a plugin I decided to build my own functionality to deliver what this organisation wants.

I have built this functionality using three plugins and a small amount of custom code in functions.php. The plugins that I used are Gravity Forms from Rocketgenius an addon for Gravity Forms User Registration and this is also available from Rocketgenius as part of their developer licencing and finally Extra User Details from Vadym Khukhrianskyi which is available in the WordPress repository. I used this plugin as it writes the additional user data to the WordPress usermeta table rather than a bespoke table as other plugins of this type do. This, in my opinion, makes for easier integration with WordPress and easier data retrieval.

The requirement I had was to create a membership number for each new user and to assign that as the WordPress username (which cannot be changed by the user through the WordPress interface). I achieved this by looking at the last user registration ID in the users table and adding 1001 to it as I needed to have 4 digit membership numbers. The username is a required field by WordPress for user registration and so needs to be populated onto the Gravity Form before it is submitted. It is therefore necessary to use the gform_ pre_validation hook in Gravity forms. The id for my form is 2 and so this is the code I added to my child themes functions.php

//Assign membership number to user
add_action('gform_pre_validation', 'pre_validation_handler');
function pre_validation_handler($form){
	if ( 2 == $form['id'] ) {
		global $wpdb;
		$result = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID DESC LIMIT 1");
		$mem_no = $result[0]->ID;
		$new_mem = 1001 + $mem_no;
		$_POST["input_5"] = $new_mem;
// have to return the form either way
	return $form;

*********** I have now changed this code due to an unforeseen problem. Please read this post too. ***********

The hidden field on the form is field 5

This is my Gravity form

The additional fields that I required were the users postal address. I therefore created the additional fields in the Extra User Fields plugin as below:


I finally mapped those additional fields to the fields in the Gravity Form as below:mem1
You will see that I have also mapped the Biography field from the Gravity Form to the standard WordPress Biography field.

Finally below you will see how the new fields are displayed at the bottom of the User Profile page:

The final part of the equation is to sort out the PayPal payment for the users membership fee. I will be using another Gravity Forms addon PayPal Payments Pro to do this when the PayPal details are made available to me. If there is anything tricky with this functionality addition to my home grown membership functionality I will add it here.

 Posted by at 3:07 pm