Removing fields for logged in users in Gravity Forms
Gravity Forms is one of my favourite WordPress plug-ins due to it’s ease of use, flexability and extensibilty. In a recent project I needed to be able to show or hide a set of fields dependant on if the user was logged in. Although Gravity Forms allows for conditional fields, these are dependant on other form fields.
As part of the registtration process, we collected numerous pieces of information, and many of the same fields would appear in other forms within the site. Why should users who have already registered and given all that infoamtion once have to do it again. In that case, there where up to 8 fields, dependant on the form, which we could hide for logged in users, but in these examples, so as to keep things nice and simple, I’ll be focusing on a single field – The users email address.
The form
To start with, you will need to create a form with at least two fields – An email address and something else (just so we know everything still works and can test it). Embed this form into a test page and test it all working. Also, make both of these fields required.
Hiding the field
To do this, we will need to add some code to the functions.php file found in your theme. If you haven’t got this file, just create it. I’ll start off by showing the code for removing the field, and I’ll then go through it and explain each part.
function remove_fields_for_logged_in_users($form) {
if(!is_user_logged_in()) return $form;
$fields_to_hide = array('Email');
foreach($form['fields'] as $key=>$field) {
if(in_array($field['label'], $fields_to_hide)) {
unset($form['fields'][$key]);
}
}
return $form;
}
add_filter('gform_pre_render', 'remove_fields_for_logged_in_users');
We start off by adding a filter to hook into the Gravity Forms core. In this case, we’re using the gform_pre_render filter, which is called nearthe start of the get_form method, which gets and renders the form. This has one parameter, which is the form object we’ll be modifying.
if(!is_user_logged_in()) return $form;
As we want to remove fields for users which are already logged in, the first thing we need to do, is check the user is actually logged in. If they’re not logged in, then we don’t have this information so we leave the form object as is.
$fields_to_hide = array('Email');
Next up, we need to declare a list of all the fields which we want to remove for logged in users. The values in this list need to correspond to the labels of the fields you used when creating the form. If you called the email field something other then “Email” you will need to change the above.
foreach($form['fields'] as $key=>$field) {
if(in_array($field['label'], $fields_to_hide)) {
unset($form['fields'][$key]);
}
}
return $form;
Lastly, we need to check if any of the fields in this form match up the ones we want to remove for logged in users. As part of the $form array passed in, there is a sub array called fields, which contains all the fields which make up this form. For each field, we check if it’s in our array of fields to remove, and if so, we remove it.
Lastly, we need to remember to return the form object.
Testing it
If you now view the form and are logged in, the email field should not be there, where as if you view the form when not logged in, both fields will be there. If you fill out both forms, they will both submit (even though the email, which is a required field wasn’t filled in for logged in users).
However, there is a problem. If you view the entries, you will see we obviously have some missing information, as the email field wasn’t shown.
Filling in the gaps
So now we need some additional functionality to fill in the missing fields, so we have all the information when viewing the form entries. To do this, we need to following code.
function add_missing_fields_for_logged_in_users($lead, $form) {
global $current_user, $wpdb;
if(!is_user_logged_in()) return;
get_current_user();
$original_form = RGFormsModel::get_form_meta($form['id']);
foreach($original_form['fields'] as $key=>$field) {
if($field['label'] == 'Email') {
$lead_detail_data = array(
'lead_id'=>$lead['id'],
'form_id'=>$form['id'],
'field_number'=>$field['id'],
'value'=>$current_user->user_email
);
$wpdb->insert("{$wpdb->prefix}rg_lead_detail", $lead_detail_data);
}
}
}
add_action('gform_post_submission', 'add_missing_fields_for_logged_in_users', 10, 2);
As before, we need to hook into the Gravity Forms core, this time using the gform_post_submission action. By the time this is executed, the form submission has already been saved. The function takes two parameters, the lead, which is the users answers to the form (plus some additional data) and the form, which is the same as before, however the form may have been modified using the above filter.
We need to start by making sure the user is logged in, as there would only be missing fields for logged in users, and we then get all the user details for the current logged in user.
$original_form = RGFormsModel::get_form_meta($form['id']);
As the form which is passed in has gone through the gform_pre_render filter, it may be missing certain fields for logged in users, in this case, the email address field. So that we have the original form, we need to get it directly, using the form ID we’re passed in as part of the form.
foreach($original_form['fields'] as $key=>$field) {
if($field['label'] == 'Email') {
$lead_detail_data = array(
'lead_id'=>$lead['id'],
'form_id'=>$form['id'],
'field_number'=>$field['id'],
'value'=>$current_user->user_email
);
$wpdb->insert("{$wpdb->prefix}rg_lead_detail", $lead_detail_data);
}
}
For each field, check if it’s one of the one we removed. If it is, we need to manually add it to the database linking it to the users lead. Each lead detail as linked to the form and the lead, we then have the ID of the field and the value, we we get from the user object.
Making this more flexible
Soon, I will update this to show you how to make it even more flexible.



