I'm trying to make it work, I found this snippet here on stackoverflow, and it working very well, I did some changes to build an drop down notifications box with bell icon as shown in the picture below from my blog.

enter image description here

This code runs one time when new post is published, and if there is new post! removing the old notification to push the new one, as you can see in the picture I got only one count(1) even if there are more than one posts is published.

So, How can i stop Cookies from removing old notifications when new post published!

How can i keep them until the expiry or clicking on the hide button?


jQuery(document).ready(function($) {


        if ($.cookie( 'hide_post_cookie' ) ) { 
            $.cookie( 'hide_post_cookie', null ) 
        var post_id = parseInt( cookie_Data.post_id, 10 );

        $.cookie( 'hide_post_cookie', post_id, { expires: 2, path: '/' } );



function.php > Check for post status.

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
    //Check if our post status then execute our code
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        if ( get_option( 'new_post_notification' ) !== false ) {

            // The option already exists, so we just update it.
            update_option( 'new_post_notification', $post );

        } else {

            add_option( 'new_post_notification', $post );


}, 10, 3 );

function.php > Get the new_post_notification and hold them with cookie.

function get_new_post_notification_bar() {

    // Get the new_post_notification which holds the newest post
    $notification   = get_option( 'new_post_notification' );

    $counter = 1;

    // Get the post ID saved in the cookie
    $cookie_post_ID = isset( $_COOKIE['hide_post_cookie'] ) ? (int) $_COOKIE['hide_post_cookie'] : false; 

    $output = '';

    if( false != $notification ) {

        //First check if we have a cookie, if not, show the notification bar
        // If a cookie is set, do not display the notification bar
        if( false === $cookie_post_ID ) {

            //Get the post's gmt date. This can be changed to post_date
            $post_date = strtotime( $notification->post_date_gmt );

            //Get the current gmt time
            $todays_date = current_time( 'timestamp', true );

            //Set the expiry time to two days after the posts is published
            $expiry_date = strtotime( '+2 day', $post_date );

            //Show the notification bar if the expiry date is not yet reached
            if( $expiry_date > $todays_date ) { 

                $output .= '<a id="notification_hide_button">';
                $output .= '<div class="notifications_bar">';
                $output .= "$counter";
                $output .= '</div>';
                $output .= '<i class="fa fa-bell" aria-hidden="true"></i>';
                $output .= '</a>';



             * If a cookie is set, check the cookie value against the post id set as last post
             * If the two don't match, delete the cookie and show the notification bar if a new post is published
             * This code only run once, that is when a cookie is still set, and new post is published within the time
             * in which the cookie is still set
            if( (int) $notification->ID !== $cookie_post_ID ) {

                        jQuery(document).ready(function($) {

                            $.removeCookie('hide_post_cookie', { path: '/' });


                $output .= '<a id="notification_hide_button">';
                $output .= '<div class="notifications_bar">';
                $output .= "$counter";
                $output .= '</div>';
                $output .= '<i class="fa fa-bell" aria-hidden="true"></i>';
                $output .= '</a>';


    $counter ++;
    return $output;

  • Note that by using the options API, only one user can use notifications and the system falls apart as soon as a second user logs in, or even a second browser – Tom J Nowell Dec 29 '18 at 14:34

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.