They can also be used for much simpler tasks, such as storing information about when a user has completed a form. In the example below, I’m going to show how to set a cookie in the user’s browser when they fill out a Gravity Form.
Setting a cookie
Gravity Forms has an action hook called gform_after_submission, which fires after a form has been submitted and committed to the database. We’re going to use that action hook to set the cookie, so that we know when a user has submitted the form. gform_after_submission can have a number appended to it to apply to a specific form, so in this case, I’m targeting the form with an ID of 1. This belongs in your functionality plugin:
The setcookie function is a PHP function, and the parameters in the function above are as follows:
- form-1-complete – the cookie name. This is a descriptive name that you will reference when checking for the cookie, as you’ll see below.
- 1 – this is the value of the cookie. I’m setting it as a 1 to notate that the form has been completed (as opposed to a 0).
- strtotime( ‘+30 days’ ) – this is how long the cookie will be stored. Using strtotime allows you to set a time relative to when the function runs, as opposed to a fixed time, such as Dec 31, 2020. So in this example, the cookie will expire 30 days after the user completes the form.
- The other four parameters you shouldn’t have to worry about, unless you want to specify whether the cookie should be available on HTTP or HTTPS exclusively.
Now every time form 1 is filled out, a cookie with the name form-1-complete and a value of 1 will be set in the user’s browser and it will expire in 30 days.
This is just an example of how to set a cookie, but you could apply to many more situations by tapping into the appropriate action hook.
Checking for a cookie
Now that you’re able to set cookies at will, you’re going to want to use them to perform an action at an appropriate point, perhaps to display a notice, or to provide access to previously restricted material.
To check whether a cookie is set in the user’s browser, we can look whether $_COOKIE['form-1-complete'] is set. Note that form-1-complete is the cookie name we specified above.
In the example below, I’ve combined checking for the cookie with a couple of other checks to hide premium content, unless all checks are met. So, if the post has a meta_key of ‘protected’ set to ‘yes’ (so that standard content won’t be affected), the user isn’t logged in (so that admins can always see the content) and the cookie ‘form-1-complete’ isn’t set (the form has not been filled out), then the user will have the content hidden from them, and a message displayed instead, indicating that they need to fill out the form. Otherwise, if any of those variables aren’t met (for example, if the user has filled out the form, meaning that the cookie form-1-complete is set and the user is a subscriber to your newsletter), then the content will be shown without restriction.
This is a powerful tool when you don’t want to rely on adding the information to the user’s profile (so that visitors are accounted for as well) to store information about a user and then use it to perform an action of some sort.
Of course, it’s not without limitations: cookies can be deleted and some users don’t even allow cookies to be set in their browsers. Then there’s also the EU (and maybe other jurisdcitions) cookie law that needs to be conformed to. So, consider this a tool in your belt that will work in some instances, but not in others.