Add update notification bubble to admin menu item?

I have re-worded this to make more sense.

Ok, I have a plugin that uses a remote service that check for updates, much like default WordPress plugins, in this case it just checks an XML file.

Read More

I want to show a menu bubble like this enter image description here when an update is available.

It can show just a “1” or text like “alert”, it doesn’t matter.

Since my plugin uses an options page (using add_options_page) the plugin settings show up under the default “Settings” submenu.

I think I need to add the following CSS to get the bubble to show up,

<span class='update-plugins count-1' title='title'><span class='update-count'>1</span></span>

and tie into the global $submenu. The problem is I cannot use a hard-coded array value for the menu since each site will have different values.

So I cannot use $submenu[80][10] .= <span class='update-plugins count-1' title='title'><span class='update-count'>1</span></span>

How can I find my plugins submenu value, do I have to loop through the array and match the string values?

Also even when I hard-coded the values I could not get the bubble to show up.

//adding plugin to menu
add_action('admin_menu', 'sec_plugin_checker');

function sec_plugin_checker() {
  add_options_page(' Plugin Checker', 'Plugin Check', 'activate_plugins', 
  'sec_plugin_check', 'sec_checker');


// the conditional where I want the bubble to appear

if (!empty($matches)){ 
    echo "Match found !<br />";

    global $submenu;
    foreach( $submenu as $item ) {

    $item[41][20] = sprintf( __( 'Updates %s', 'sec_plugin_checker' ),  
                "<span class='update-plugins count-1' title='title'>
                 <span class='update-count'>1</span></span>");  

       }
  }

and here is what a var_dump($submenu); looks like,

["options-general.php"]=>
array(9){
...

[41]=>
    array(4) {
      [0]=>
      string(20) "Plugin Check"
      [1]=>
      string(16) "activate_plugins"
      [2]=>
      string(21) "sec_plugin_check"
      [3]=>
      string(23) " Plugin Checker"

     ...

      }

Related posts

Leave a Reply

2 comments

  1. 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.

  2. Or something along these lines – adapt as needed:
    (this is to 1) add a menu item for Gravity Forms and 2) show the count of all unread form IDs in the array. You can extend this with submenu pages for specific forms, with their own counts if needed.

    add_action( 'admin_menu', 'register_my_custom_menu_page' );
    
    // let's build the function
    function register_my_custom_menu_page() {
    
        $search_criteria = array(
            'status'     => 'active', // Active forms 
            'field_filters' => array( // which fields to search
                array(
                    'key' => 'is_read', 'value' => false, // let's just get the count for entries that we haven't read yet.
                )
              )
            );
    
        // Add the form IDs to the array below, the parent menu will show ALL unread entries for these forms
        // NOTE: you can get counts for whatever you're wanting and change the value of $notification_count below accordingly. 
        $notification_count = GFAPI::count_entries( array(1,4,5,6,11,13), $search_criteria );
        
        add_menu_page(
            'Full Quote Form submissions', // Page Title
            $notification_count ? sprintf( 'Quotes <span class="awaiting-mod">%d</span>', $notification_count ) : 'View Quotes',
            'manage_options', // Capabilities 
            'admin.php?page=gf_entries&id=13', // menu slug
            '', // callback function
            'dashicons-format-aside', // icon URL
            6 // position
        );
    }