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.

Comments

  1. Dolenec says:

    This is exactly what I was looking for.
    I was just wondering, is there a way to use Gravity data to register new user?

    Thanks for the great post ;)

    • admin says:

      Dolenec, Glad you found this post useful

      You could use Gravity Forms in place of a registration form using the gform_post_submission hook and the wp_insert_user function (http://codex.wordpress.org/Function_Reference/wp_insert_user)

      You could then use the email notification to send the user a welcome email and ooptionally an email notification to yourself.

      There are however a few things to consider:
      1. I’m not 100% sure hhow Gravity Forms handles password fields, but you’re likely to have the plain text password in the database, which is a security risk
      2. You’re making more work for yourself this way. I assume the reason you ask is because you want to add some additional fields to the registration process? If so, you may want to check out the Register Plus plug-in – http://wordpress.org/extend/plugins/register-plus/

      Hope this helps

  2. brk41 says:

    searching for this for months:) thanks…
    i have a question. i want to hide multiple fields if user logged in. For example i want to hide name, email, password and html fields. How can i do that.

    Thanks for great post.

    • admin says:

      The $fields_to_hide variable can contain the names of multiple input elements.
      As we loop over each field in the form, we check if that is int he array. This means we can hide multiple fields, or have multiple possible labels for the same thing (email, e-mail, email address etc) and it will then remove any variation of “email”

      Hope this clears things up

  3. Mistry says:

    Damian, thanks for a great post – I’m in the processing of creating a site with much user generated content based on a wordpress framework – please could you drop me a line and let me know your rates as I could probably implement these changes but I think I would probably need help with the more heavy duty stuff!

    Thanks

    • admin says:

      Glad you found it useful. I’ve sent you an email, so will be interesting to find out more about this project of yours :-)

  4. CC says:

    Damian, great post! How can I apply conditional logic to a text input field and check if value is equal? So if user inputs “yes”, then show dropdown list.

    Thanks!

    • admin says:

      Gravity Forms supports conditional logic, meaning it will only show certain form elements if certain conditions are met.

      The question is, why would you want to do it based on the contents of a free text field? Would it be better to have radio options/dropdown, and depending on if they select Yes from that, show some additional fields.

      To do this, for the fields which are conditional, edit them and select the Advanced tab. Then check “Enable conditional logic”. You can then choose to only show this field when a dropdown, radio buttons or checkbioxes meet a certain condition.

  5. Jan Zikmund says:

    Hello Damian, thanks a lot for your post, it is exactly what I was looking for. Unfortunatelly this doesn’t work for required fields. If I remove a field this way and the field is marked required, after form submission I just get error “There was a problem with your submission. Errors have been highlighted below.” – of course no errors are shown, but the form just doesn’t submit. For non-required fields if works as expected. Don’t you know where the problem could be ?

    Thanks a lot

  6. Bob says:

    Does this work and for required fields? I want to submit form but if I hide required fields, I can’t submit the form until I show the fields and fill them…

Leave a Reply

Your email address will not be published. Required fields are marked *