Display the number of unseen comments on a page since the user last visit

The community is private and it has several pages /wordpress /design /etc . When users go to those pages the only thing they can do is to comment.

Now, what I am trying to do, Idk if possible, but if it is I need some directions/hints on what too search for.

Read More

http://jsfiddle.net/melbourne/uPqBe/4/

Instead of those numbers I want to display like the title says, the number of lastest comments since the current user visited those pages.

I thought of using localStorage/cookies but then if the user decides to login from somewhere else, that won’t do the job, in others words I dont have a clue on where to go from here.

Any suggestions would really be appreciated.

Related posts

1 comment

  1. You can store the data in the user meta as an array of post-id -> comment count at last visit and then simply count the comments since that date, for example

    function get_user_comment_count_since_last_visit($user_id ,$post_id){
        //only do this for logged in users
        if ($user_id <= 0 ){
            return 0;
        }
    
        /**
         * get last comment count for a post id from user meta if set
         * and if not set then we define zero
         */
        $user_last_visit = get_user_meta( $user_id,'_last_visit_',true);
        if (!isset($user_last_visit[$post_id]) || $user_last_visit[$post_id] < 0)
            $user_last_visit[$post_id] = 0;
    
        /**
         * get current comment count of the post
         */
        $comments_count = wp_count_comments( $post_id);
    
        /**
         * get the amount of added comment since last visit
         * and update the user meta for next visit
         */
        $user_last_visit[$post_id] = $comments_count->approved -  $user_last_visit[$post_id];
        update_user_meta( $user_id, '_last_visit_',  $user_last_visit);
        return $user_last_visit[$post_id];
    }
    

    and to use it you call it with the page id and user id of the current logged in user.

Comments are closed.