Checking for cookies returns correct result only inside Elementor Editor

admin2025-04-18  1

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);
        wp_send_json("redirecting");
    }
    wp_die();
}

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'];

    if(isset($_COOKIE[$terms])) 
    {
        return 1;   
    } 
    else
    {
        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 ?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744950213a276287.html

最新回复(0)