Add update notification bubble to admin menu item?

I would do this when you call add_options_page(), not later. It’s always better to do this with the supported API instead of playing with the internal structures.

The plugin updater periodically checks the plugin status and then saves the result in a transient. This means that it only reads this cached status when the menu is created, it doesn’t do the full check on every page load. You can do something similar:

add_action( 'admin_menu', 'wpse15567_admin_menu' );
function wpse15567_admin_menu()
{
    $warnings = get_transient( 'wpse15567_warnings' );
    $warning_count = count( $warnings );
    $warning_title = esc_attr( sprintf( '%d plugin warnings', $warning_count ) );

    $menu_label = sprintf( __( 'Plugin Checker %s' ), "<span class="update-plugins count-$warning_count" title="$warning_title"><span class="update-count">" . number_format_i18n($warning_count) . "</span></span>" );

    add_options_page( 'Plugin Check', $menu_label, 'activate_plugins', 'sec_plugin_check', 'sec_checker' );
}

Menu item with notification bubble

When you do the actual warning check, you save the results in the transient so it can be read later:

if ( ! empty( $matches ) ) {
    set_transient( 'wpse15567_warnings', $matches );
}

Notice that I don’t do anything special when there are no warnings. The bubble doesn’t get displayed because it gets the class count-0, which has display: none in the css.

Leave a Comment