I'm having trouble finding a solution for this:

I have a bunch of posts with restrict content, and to access the content of these pages the user has to submit a gravity form. I imagine the best way to do so is to set a cookie when gform_after_submission takes place. Then a function would check for that cookie and display this or that depending on the result. The thing is: I need a cookie for every single post rather than a single cookie for the whole site. This is what I have now:

Creating cookie after submission. My idea here was to use the post id to name the cookie and make it unique. After this but I'm not getting anywhere from here.

add_action( 'gform_after_submission_1', 'create_cookie' );
function create_cookie() {
    setcookie( 'cookie'.get_the_ID(), 1, strtotime( '+30 days' ), COOKIEPATH, COOKIE_DOMAIN, false, false);

Checking if that cookie exists. So after submission the page will reload and the cookie being set it would be detected by the browser and things would happen.

add_filter( 'the_content', 'checkingCookie' );
function checkingCookie($content) {
  if( !isset( $_COOKIE['cookie'.get_the_ID()] ) ) {
     return 'no cookies!';
  else {
     return 'cookies!';

The thing is that this particular cookie created in a given page should only be detectable in its origin post so that if I go to another post the content there would still be restricted and waiting for its own submission to liberate content.

so, any ideas?


You can accomplish what you're after by first creating a hidden field in your form. Set the value to the current embed page:

enter image description here

Take note of the hidden fields ID, for this demo, it's 3.

Then your code for the content is similar to what you had:

add_action( 'the_content', function ($content) {

    // check if user has submitted this pages form, if not, show only form
    if( !isset( $_COOKIE['unrestrict_'.get_the_ID()] ) ) {

        // get form #1
        $form = RGForms::get_form(1);

        // uncomment to review cookies
        // echo "<pre>".print_r($_COOKIE,true)."</pre>";

        return '<h2>Restricted Content - Fill out Form</h2>'.$form;
    } else {

        // user has in last 30 days submitted this pages form
        // show content
        return $content;

For the processing, we'll do

add_action( 'gform_after_submission_1', function ($entry, $form) {

    // uncomment to review the entry
    // echo "<pre>".print_r($entry,true)."</pre>"; die;

    // get the hidden field, the embedded from page
    $from_page = rgar( $entry, '3' );

    // set the cookie
    setcookie( 'unrestrict_'.$from_page, 1, strtotime( '+30 days' ), COOKIEPATH, COOKIE_DOMAIN, false, false);

    // redirect so we dont land on gravity forms "thank you" page
    wp_redirect( get_permalink( $from_page ) );

}, 10, 2);

If you want this to be on some pages, but not all, inside of the_content filter you could add your conditionals for the page ID you do/n't want. And you could explore using a checkbox with post_meta/metabox.

You can choose to store one cookie per-page like above, or you could do one main cookie where the value is a serialized array of ID's that you add and look for.

afaik using cookies this simply isn't secure, they can be faked. So if you're hiding delicate info you should review authentication, secure cookie storing, and cookie/user verification. If this is what I presume it's for, lead captures, this is a fine approach.

I've used anonymous functions in the actions, if this is a public plugin/theme you may want to call the function instead.

  • Thank you so much for your solution! It's a thing of beauty. :) – Paulo Gabriel Jan 26 '18 at 12:50

David's solution is perfectly fine just including this here as an alternative if you want to avoid touching code:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.