Skip to:
Content
Pages
Categories
Search
Top
Bottom

Red dot in nav-bar when new entry on activity stream


  • itsenaf
    Participant

    @gamerz36

    Hello,

    I try for hours to create a function, which will display a small red dot on the nav-bars acitivity stream link, when someone posted something new. Since all users are logged in, it should be possible to work with metadata “last_activity_visit”.
    Example screenshot: https://i.imgur.com/eeCvvkj.jpeg
    What I tried:
    Added below code to themes functions.php. Actually managed to display a red dot, when trying around with the code. But either the dot won’t be displayed at all, or it’s displayed always, even when the user visited the acitivity stream.

    
    function track_activity_page_visit() {
        if (bp_is_activity_component()) {
            update_user_meta(get_current_user_id(), 'last_activity_visit', current_time('mysql'));
        }
    }
    add_action('wp', 'track_activity_page_visit');
    
    function has_new_activity_posts() {
        $last_visit = get_user_meta(get_current_user_id(), 'last_activity_visit', true);
    
        if (empty($last_visit)) {
            return false;
        }
    
        $args = array(
            'date_query' => array(
                array(
                    'after' => $last_visit,
                ),
            ),
            'max' => 1
        );
    
        $activities = bp_activity_get($args);
    
        if (!empty($activities['activities'])) {
            return true;
        }
        
        return false;
    }
    
    function add_red_dot_to_menu_buddypress_activity($items, $args) {
        if (is_user_logged_in() && has_new_activity_posts()) {
            foreach ($items as &$item) {
                if (strpos($item->url, '/activity/') !== false) {
                    $item->title .= ' <span class="red-dot"></span>';
                }
            }
        }
        return $items;
    }
    add_filter('wp_nav_menu_objects', 'add_red_dot_to_menu_buddypress_activity', 10, 2);
    

    Any clue? 🙂

  • You must be logged in to reply to this topic.
Skip to toolbar