Add a Pending Post Count Bubble to the WordPress Admin Menu

When building a WordPress plugin or a custom back-end workflow, it is often useful to show a small count bubble next to an admin menu item. WordPress does this for comments, plugin updates, and theme updates, but there is no dedicated API for every custom scenario.

After looking through several existing implementations, it turns out there is more than one way to build this feature.

Method 1: use the add_menu_classes filter

This is the most flexible solution when you want to attach a notification bubble to an existing admin menu item, especially in a theme or a custom content workflow.

add_filter('add_menu_classes', 'show_pending_number');

function show_pending_number($menu) {
    $type = 'animals';
    $status = 'pending';
    $num_posts = wp_count_posts($type, 'readable');
    $pending_count = ! empty($num_posts->$status) ? $num_posts->$status : 0;

    if ($pending_count <= 0) {
        return $menu;
    }

    foreach ($menu as $key => $value) {
        if ($menu[$key][2] !== 'edit.php?post_type=' . $type) {
            continue;
        }

        $menu[$key][0] .= ' <span class="awaiting-mod update-plugins count-' . $pending_count . '">'
            . '<span class="pending-count">' . number_format_i18n($pending_count) . '</span>'
            . '</span>';
    }

    return $menu;
}

The basic idea is simple: count how many posts are in the target status, find the menu item you want to enhance, and append the same badge markup WordPress already uses for update counts.

The AnsPress approach

Some plugins build the count right into the menu label when they register the top-level menu page. AnsPress is a good example of that pattern.

add_menu_page(
    'AnsPress',
    'AnsPress' . $counts['total'],
    'delete_pages',
    'anspress',
    array($this, 'dashboard_page'),
    ANSPRESS_URL . '/assets/answer.png',
    $pos
);

This is convenient when you fully control the menu registration yourself.

The WooCommerce approach

WooCommerce adjusts the submenu data after registration and appends the processing order count to the right menu item.

if (
    apply_filters('woocommerce_include_processing_order_count_in_menu', true)
    && current_user_can('manage_woocommerce')
    && ($order_count = wc_processing_order_count())
) {
    foreach ($submenu['woocommerce'] as $key => $menu_item) {
        if (strpos($menu_item[2], 'wc-orders') === false) {
            continue;
        }

        $submenu['woocommerce'][$key][0] .= ' <span class="awaiting-mod update-plugins count-' . $order_count . '">'
            . '<span class="processing-count">' . number_format_i18n($order_count) . '</span>'
            . '</span>';
    }
}

If you only need a general-purpose solution, the add_menu_classes filter is usually the easiest choice. But when your plugin owns the menu itself, the AnsPress or WooCommerce style can be cleaner.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *