Sep 232014

I found a small problem with the way I was generating the membership number, the solution I documented on this post works BUT if a member registers but then fails to activate their membership before another person registers they both have the same membership number and user name. WordPress, rightly, does not allow two members to have the same user name.

I therefore created a custom table in the WordPress database called membership. It has two fields ID and member, ID is set to 1 and the member field is set to one less than whatever you want the starting membership number to be. The code in functions.php therefore needs to be changed to read and write the membership number to the custom table. Rather than use a custom table I could have used the options table but I thought it better to have the custom table so that it is easier to edit with phpmyadmin if necessary. The code now looks like:

add_action('gform_pre_validation', 'pre_validation_handler');
function pre_validation_handler($form){
	if ( 2 == $form['id'] ) {
		global $wpdb;
        $myrows = $wpdb->get_row("SELECT * FROM membership WHERE ID = 1" );
        $member = $myrows->member;
        $member ++;
        $wpdb->update('membership',array('member' => $member),array('ID'=> 1));
        $_POST["input_5"] = $member;
// have to return the form either way
	return $form;

This now works correctly but it does mean there may be gaps in membership numbers should members who register not activate their accounts.

 Posted by at 5:59 pm
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
Mar 122014

I am currently developing a WordPress website for a genealogical organisation. One of the things they wanted was for any website visitor to be able to complete a contact form and for the content of the form to go direct to their member and not to the WordPress Admin email address.

The first thing to do was to create the contact form in Gravity Forms. This was just a simple form requiring first and last names, email address and message text. To this form I added a hidden field to store the email address that the form should be submitted to. I also created a custom notification to go along with this form. I called the notification ‘Profile Contact’. It does not matter which of the ‘Send To’ options you choose in this notification.

The next thing was a bit more complicated and it caused me to have an email exchange with Chris and Richard at Gravity forms support to get this up and running.

The page that accesses this contact form is based on a customised page template. This is the relevant piece of code that calls the contact form

<div class="cs_detail">		
<div class="cs_detail_header">Contact Details:</div><!--/cs_detail_header-->
<div class="cs_detail_content">
For more information please contact: 
$_SESSION['member'] = $study_member;
$contact = $wpdb->get_row("SELECT * FROM members WHERE number = '$study_member'");
$details = $contact->title." ".$contact->firstname." ".$contact->surname;								
echo " <b>". $details."</b>  ";
<button class="eModal-1">Send Email</button>

This code needs a little explanation, the session variable is used to pass the result of another query on another custom table over to the custom Gravity Forms function.  The button is provided by the Easy Modal plugin that I use to display the contact form as a modal window over the page.

Two functions need to be added to the Themes functions.php file.

The first one is:

/** Set the email to address on the contact form **/
add_filter('gform_notification', 'change_autoresponder_email', 10, 3);
function change_autoresponder_email( $notification, $form, $entry ) {

    // There is no concept of user notifications anymore, so 
    // we will need to target notifications based on other 
    // criteria, such as name
    if($notification["name"] == "Profile Contact"){

        // toType can be routing or email
        $notification['toType'] = "email"; 
        // change the "to" email address
        $notification['to'] = $entry["4"];


    return $notification;

The code is copied straight from the Gravity Forms documentation here. I used the code in Example 2 and just changed the value of the $entry to 4 as this was the field number of the  hidden field. I also changed the $notification to reflect the name of my custom notification form. (Don’t forget to delete the default notification or you will still get emails to the WordPress Admin account too).

The code for the second custom function is

// will populate a field in your form which has a parameter name of 'email_to'
add_filter("gform_field_value_email_to", "populate_email_address");
function populate_email_address($value){
	global $wpdb;
	$study_member = $_SESSION['member'];
	$contact = $wpdb->get_row("SELECT * FROM members WHERE number = '$study_member'");
	$email = $contact->studyalias1."";
	return $email;

There is some explanation of this code required. As I am connecting to the WordPress database to get the email address from a custom table the global $wpdb variable needs to be added to the code. The membership number variable ($study_member) is populated from the session variable and then used in the query. The email address is constructed and then passed back to the Gravity Form in the hidden field. To prove this you can put it in a visible field on the form for testing purposes.

This works exactly as required by my organisation and shows the versatility of Gravity Forms.

Other useful links relevant to this post

Easy Modal plug-in.

Gravity Forms plug-in.

Gravity Forms documentation on the entry object.

 Posted by at 5:08 pm