I want to allow the user to download the posts(show list of posts) only when they have submitted the lead gen form, which in turn adds the cookies in their browser.

Now whenever someone fills the lead gen form, there is an AJAX request send which does the following:

add_action( 'wp_ajax_nopriv_set_lead_cookie_and_mail', 'mail_and_cookie_function' );
add_action('wp_ajax_set_lead_cookie_and_mail', 'mail_and_cookie_function');
function mail_and_cookie_function(){
    check_ajax_referer( 'aj-demo-nonce', 'nonce' );  
    parse_str($_POST['form_data'], $form_data); // This is the new added line
    $name =  $form_data["wpcf-lead-name"];  // This is how you call the field.
    $email =  $form_data['wpcf-lead-email'];
    $number = $form_data["wpcf-lead-number"];
    $class =  $form_data["wpcf-class"];
    $category =  $form_data["hidden_category"];

    if(!$_COOKIE[$category]) {      
        setcookie($category, "1", time()+2592000);

Now when the cookie is saved the website would redirect to the page where I want to check what all cookies are saved to display the content.

This is my shortcode content to check cookie values.

add_shortcode('check_cookies', 'check_cookies_for_category');

function check_cookies_for_category($atts){
    $atts = shortcode_atts(     array(
    'taxonomy' => '',
    ),  $atts

    $terms = $atts['taxonomy'];

        return 1;   
        return $terms;

When I view this shortcode inside my elementor editor, it gives back correct result. "1" And once I check it on the live website, it says that the cookies are not set. ? "science" <= cookie term value How can I make it work on the frontend website as well ?

  • when do you define the cookie ? give more details about how you use it. – Kaperto Dec 2 '19 at 6:54
  • I define cookies when someone submits the form. An ajax request is made, which does the basic check and thereupon, I set the cookie. this the part of the Ajax action function if(!$_COOKIE[$category]) { setcookie($category, "1", time()+2592000); wp_send_json("redirecting"); } wp_die(); – Bhanu Dec 2 '19 at 7:01
  • which form ? where ? why ? edit your question to give more details because I don't understand what you want to do. – Kaperto Dec 2 '19 at 7:19
  • Hey @Kaperto i have edited the question. – Bhanu Dec 2 '19 at 7:56
  • the problem comes because you don't set the path on setcookie then the cookie is only available in the directory "wp-admin". moreover the cookie can be create in the browser then it's more secured if you use PHP sessions for that. – Kaperto Dec 2 '19 at 8:24

Your Answer

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

Browse other questions tagged or ask your own question.