Introducing WordPress…

I’ve been working more and more, over the last 6 months or so, with WordPress for one of my larger clients.

As I’m sure you’re aware, WordPress is a blogging platform come entire website framework, which is it seems more and more companies are turning to in order to create the basis of their websites. No doubt due to the free nature of the framework, along with the sheer amount of support available.

One of the great things about WordPress, is it’s customisability.. There are quite literally hundreds of thousands of themes, widgets and plugins available, which can turn the vanilla WordPress install into a fully functioning e-commerce solution for instance.

However…. I digress slightly….. Back to the purpose of this post….

My client specialises in events, organisation and management, and they use WordPress sites for the Publicity, Information and Registration side of things.

Introducing Gravity Forms..

One of the most useful plugins from an event registration standpoint is Gravity Forms. Gravity Forms allow the site owner to create a series of input elements forming any sort of form they like. Elements include Text Boxes, Radio Buttons Checkboxes etc etc;

Facebook Registration - Editor

Using this plugin, my client is able to create the structure required to allow guests to register for an event. Gravity forms allows for customisation of the Confirmation messages, and will even email out once a form has been completed. Fields can be “routed” allowing only certain parts to be shown depending on the entries in other fields. They can also be pre-populated from information stored in the users’ profile, lessening the time required for guests to fill out forms. All the entries can then be exported to a csv file, and analysed in Excel.

If you want to find out more, hit their website up and explore!

Registration Site UX…

However, Gravity Forms will only get you so far, and inevitably, you’ll reach a limit to it’s flexibility. Which is exactly where I’ve found myself on quite a few occasions lately, and as such, I’ve need to put my favourite programmers hat on, and start tinkering!

The first bit of functionality my client asked for will be the theme of this first post, and I’ll then go ahead and make a further few posts detailing some of bits I’ve created, in the following few weeks.

As part of an Event Registration site for Facebook, we had a basic “Are you Attending?” style registration form, which took the guests Name, Company, Job Title, their Email Address, and whether they were attending or not;

Facebook Registration

Guests were pre-registered for the site, that is, my client had been supplied each guests’ Name and Email address. However, the site was designed to be fully open, so guests shouldn’t need to login at all…. However, my client needed the Form to only accept Attendance submissions from people who had been pre-registered on the site.

Of course, WordPress will allow you to lock pages so that only logged in users are able to view them…. But this wasn’t the UX my client wanted.

As this wasn’t something that Gravity Forms was able to do either, I needed to come up with an alternate coded solution.

Gravity Forms API…

Like a lot of things in the WordPress space…. GravityForms  is designed to be extensible. As such, GravityForms provides a Handy API for developers like me to hook into.

What I needed to do was to match the email address the guest enters in the registration for to the list of pre-registered users stored in the WordPress Users Table.

My first thought was to use the gform_pre_submission_filter filter and prevent the form from being saved to the database… But this wasn’t ever going to be the best way to go about this as I wasn’t able to deliver any meaningful response back to the user….

What I really needed to do was to hook into the Forms’ Validation Filter so that I could return some meaningful feedback to the guest, that their entry wasn’t valid.

Enter the Gravity Forms Validation Filter…

GravityForms provides a handy filter; GForm Validation allowing you to grab a form before it’s submitted, check its values, and return a validation result based upon your own logic.

[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
<?php
add_filter("gform_validation", "custom_validation");
?>
[/sourcecode]

Where the first parameter of the add_filter function is the name of the filter to hook into, the second parameter names our custom validation function name.

The GravityForms Documentation Page lists this Filters’ description as;

Use this filter to create custom validation logic.“…

Handily, the documentation provided an example to prevent a specific number from being entered into a field, which obviously gave me a head start with my coding.

This Filter allows us to return a validation result based upon whether or not the Users email address exists in the pre-populated WordPress Users table based on the email address the guest enters while registering.

What we want here is to let the guest know if there email address isn’t valid, right on the form itself, and under the email field;

Facebook Registration - Validation

Our Custom Validation Function…

Once we’ve added our filter definition, we obviously need to create the bare bones of our Custom Validation Function;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]function custom_validation($validation_result) { }[/sourcecode]

The first thing we need to do is get a reference to the Form being validated. This is passed in within the $validation_result parameter. We can get the Form by using the following code;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]$form = $validation_result["form"];[/sourcecode]

The WordPress get_user_by Function…

The next thing we want to do is search for users based upon the email address field. WordPress provides a handy function for this; get_user_by(); – which is described in the codex as “Get user data by field and data. The possible fields are shown below with the corresponding columns in the wp_users database table“.

This function accepts a field name and a value to search for within that field, and returns either a user if the search pattern matches an entry, or nothing if no match is found.

So we need to pass in the email address entered by the guest into this function somehow…

Gravity Forms Form Variable ID’s…

Each Form Element in a Gravity Form Form is assigned a unique ID, allowing us to reference it and ascertain it’s value. The Form Element ID can be found at the top of the Element container when you are editing it, as shown below;

Facebook Registration - Editor - Field IDs

POST Variables…

When a Form is submitted, each Elements’ value is sent back to the server using POST. This means we have access to all these values within our Validation Function using the $_POST variable… This variable is known as a Superglobal, and are described in the php manual as;

“Several predefined variables in PHP are “superglobals”, which means they are available in all scopes throughout a script.”

Now that we know the Field ID of the Email Element on our Form, in our case it’s ID 3, we can grab it’s value from the $_POST variable;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$_POST["input_3"]
[/sourcecode]

Let’s see if there’s a matching User…

Combining this piece of code with the get_user_by described above we can search for Users based on the email address;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$user = get_user_by( ’email’, $_POST["input_3"] );
[/sourcecode]

If the function finds a User, the object will be stored in the $user variable, and if no Users are found then this variable will be null.

What we want to do is, if there’s a user found in the database that matches the Email Address the guest has entered, then allow the form to be submitted. However, if no User is found, then we want to fail the Validation and prevent the submission.

The beginnings of this are;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
if ($user != null) {
// Do nothing
} else {
// Set the email field validation to invalid
}
[/sourcecode]

Finding the Email Field…

We’ve already gotten a reference to our Form at the start of our Validation Function;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]$form = $validation_result["form"];[/sourcecode]

We now need to loop through all the Form Fields until we find the Email Field, which we already know has an ID of 3;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
// Finding Field with ID of 3 (The Email Field) and marking it as failed validation
foreach ( $form["fields"] as &$field ) {
if ( $field["id"] == "3" ) {
break; // We’ve found the Form Field…. Exit the Loop
}
}
[/sourcecode]

You’ll notice that we’ve preceded the $field variable in our foreach declaration with an “&”. This allows us to directly access the variable “by reference”. The PHP manual says;

“In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.”

This is just shorter alternative to something like;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
// Finding Field with ID of 3 (The Email Field) and marking it as failed validation
foreach ( array_keys( $form["fields"] ) as $key ) {
if ( $form["fields"][$key]["id"] == "3" ) {
break; // We’ve found the Form Field…. Exit the Loop
}
}
[/sourcecode]

Setting and Returning the Validation Result…

In order to let the Form know whether we’ve failed the Validation, we set the $validation_results “is_valid” property to false;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$validation_result["is_valid"] = false;
[/sourcecode]

Adding the Validation Message…

First we need to We now need to set the Validation Message against the Email Field. Each Form Field has a “failed_validation” property which allows us to set whether this Field has failed our validation;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$field["failed_validation"] = true;
[/sourcecode]

We then need to set the Message that appears against the Form Field, using the “validation_message” property;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$field["validation_message"] = "Thank you for attempting to register. Unfortunately we cannot find your email address on our invitation list. If you are interested in attending this event please contact us using the form below.";
[/sourcecode]

Having set the Fields Validation Properties, we then need to push our modified Form object back into the $validation_result object, and return it out from our Custom Validation function;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
$validation_result["form"] = $form;
return $validation_result;
[/sourcecode]

Wrapping up…

And we’re done! Our function is complete;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”php”]
add_filter(‘gform_validation_1’, ‘custom_validation’);

function custom_validation($validation_result){
$form = $validation_result["form"];

$user = get_user_by( ’email’, $_POST["input_3"] );

//
// If we’ve found the user, then we can submit the form…
//
if ($user != null)
{
// Do nothing
}
else
{
$validation_result["is_valid"] = false;

//finding Field with ID of 3 (The Email Field) and marking it as failed validation
foreach ( $form["fields"] as &$field ) {

if( $field["id"] == "3" ) {
$field["failed_validation"] = true;
$field["validation_message"] = "Thank you for attempting to register. Unfortunately we cannot find your email address on our invitation list. If you are interested in attending this event please contact us using the form below.";
break;
}
}
}

//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}
[/sourcecode]
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

By |2017-07-24T08:33:16+01:00January 31st, 2015|Development, Gravity Forms, Wordpress|0 Comments

About the Author:

Leave A Comment