Akash Gajbhiye Akash Gajbhiye - 1 year ago 99
PHP Question

cookies not working in wordpress

I've created a meta field 'priority_priority' so that I can sort post based on how many times they have been visited..least visited post gets more priority. so whenever a visitor visits the post the priority variable is decremented.but I want priority to be decremented only on the first visit from the same user so am using cookies but the following code in not working the priority is always decremented and


always gives NULL

$post_id = get_the_ID();
$priority = get_post_meta($post_id,'priority_priority',true);
if(count($_COOKIE) > 0)
$priority = $priority-1;


<div id="primary" class="content-area">

<main id="main" class="site-main site-main--single" role="main">
<?php var_dump($_COOKIE['visitor'.$post_id]);?>
</main><!-- #main -->

</div><!-- #primary -->

Answer Source
**Add this Function in functions.php**

function set_user_cookie() {
    $test = "fyjfhjf";
    setcookie('default_school', $test, time()+3600);
add_action( 'init', 'set_user_cookie');

**To display**
echo $_COOKIE['default_school']; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download