Title: custom field set cookie
Last modified: May 11, 2021

---

# custom field set cookie

 *  [tlash](https://wordpress.org/support/users/tlash/)
 * (@tlash)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/)
 * I would like to capture the custom field input to a cookie. I have created a 
   field for custom zip using the custom field documentation example. Trying to 
   set the cookie with the field but the output is ‘ag_field_zip’ and not the value
   of the input field.
 *     ```
       add_filter('pre_age_gate_custom_fields', 'my_ag_custom_zip', 10, 1);
   
       function my_ag_custom_zip($fields){
         $fields .= '<label for="ag_field_zip" class="ag_field_zip_label">Please add your zip code</label></br>';
         $fields .= '<input type="text" id="ag_field_zip" name="ag_field_zip" class="ag_zipcode" placeholder="zip code" value="" maxlength="12" size="12"/></br>';
         $fields .= age_gate_error('ag_field_zip');
         $zip_field =  $_POST['ag_field_zip'];
   
         if(!isset($_COOKIE[ag_zip])) {
           setcookie('ag_zip', $zip_field, time()+31556926);
            }
         return $fields;
       }
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-field-set-cookie%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Phil](https://wordpress.org/support/users/philsbury/)
 * (@philsbury)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14427184)
 * Hi [@tlash](https://wordpress.org/support/users/tlash/),
 * You’re not far off there, but you probably want to set the cookie when the form
   is submitted. This assumes you’re on the PHP version:
 *     ```
       /**
        * Pretty much what you have already
        */
       add_filter('pre_age_gate_custom_fields', 'my_ag_custom_zip', 10, 1);
   
       function my_ag_custom_zip($fields)
       {
           $fields .= '<label for="ag_field_zip" class="ag_field_zip_label">Please add your zip code</label></br>';
           $fields .= '<input type="text" id="ag_field_zip" name="ag_field_zip" class="ag_zipcode" placeholder="zip code" value="" maxlength="12" size="12"/></br>';
           $fields .= age_gate_error('ag_field_zip');
   
           return $fields;
       }
       ```
   
 * Then handle the submission:
 *     ```
       /**
        * An action on the form success hook
        */
       add_action('age_gate_form_success', 'set_zip_cookie');
   
       function set_zip_cookie()
       {
           // make sure this is sanitized and/or validated
           $zip_field =  $_POST['ag_field_zip'] ?? '';
   
           if (!isset($_COOKIE['ag_zip'])) {
               setcookie('ag_zip', $zip_field, time()+31556926);
           }
       }
       ```
   
 * If you’re on the JS version, that’d be a bit different and less easy in the current
   version. Would be possible though.
 * Thanks
    Phil
 *  Thread Starter [tlash](https://wordpress.org/support/users/tlash/)
 * (@tlash)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14427250)
 * Thanks! that works great. There’s another issue but i will open separate item.
 *  Thread Starter [tlash](https://wordpress.org/support/users/tlash/)
 * (@tlash)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14432895)
 * Hi Phil,
    Turns out I do need some guidance on a JS version as our server cache
   requires i use JS.
 * When I use the code above, I see the cookie is set in the browser cookie, but
   when I inspect with dev tools it’s not there. Switching off JS and then I see
   the cookie in dev tools.
 * Thanks in advance.
 *  Plugin Author [Phil](https://wordpress.org/support/users/philsbury/)
 * (@philsbury)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14435348)
 * Hi [@tlash](https://wordpress.org/support/users/tlash/),
 * You’ll need some JS functions it you’re using the JS version, something like 
   this will work:
 *     ```
       function setCookie(name, value, days) {
         var expires = "";
         if (days) {
           var date = new Date();
           date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
           expires = "; expires=" + date.toUTCString();
         }
         document.cookie = name + "=" + (value || "") + expires + "; path=/";
       }
   
       window.addEventListener('agegatepassed', function () {
         var zip = document.getElementById('ag_field_zip');
   
         if (zip) {
           setCookie('ag_zip', zip.value);
         }
       });
       ```
   
 * I’ve rolled that code up in [this package](https://www.dropbox.com/s/5k3y6wzzgx4iduj/age-gate-fields.zip?dl=1)
   that you can copy… or just install as a separate plugin, it also has some bits
   for your [other thread](https://wordpress.org/support/topic/setting-a-field-name-for-a-custom-field/),
   but I’ll reply there too!
 * Thanks
    Phil
 *  Thread Starter [tlash](https://wordpress.org/support/users/tlash/)
 * (@tlash)
 * [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14435660)
 * Phil,
    You are awesome. thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘custom field set cookie’ is closed to new replies.

 * ![](https://ps.w.org/age-gate/assets/icon-256x256.png?rev=2783003)
 * [Age Gate](https://wordpress.org/plugins/age-gate/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/age-gate/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/age-gate/)
 * [Active Topics](https://wordpress.org/support/plugin/age-gate/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/age-gate/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/age-gate/reviews/)

## Tags

 * [cookies](https://wordpress.org/support/topic-tag/cookies/)

 * 5 replies
 * 2 participants
 * Last reply from: [tlash](https://wordpress.org/support/users/tlash/)
 * Last activity: [5 years ago](https://wordpress.org/support/topic/custom-field-set-cookie/#post-14435660)
 * Status: not resolved