Get restrict content by submitting Gravity Forms

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.

Leave a Comment

tech