Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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"

     ...

      }
share|improve this question
    
What is the reason for this? What exactly you want to do? –  JohnnyPea Apr 25 '11 at 23:07
1  
@Wyck: You are talking about the bubble that indicates the number of available updates and unmoderated comments? Maybe you should update your question to include a screenshot, to make this more clear. –  Jan Fabry Apr 26 '11 at 7:12
    
Yes exactly, I'll add the screen. –  Wyck Apr 26 '11 at 15:32
    
For what, exactly, are you wanting to indicate available updates? for WPORG repository-hosted Themes and Plugins, this update notification is handled automatically. –  Chip Bennett Apr 26 '11 at 15:43
    
It's a custom function for a plugin, its gets remote updates from another service, and I want to use the same update notification functionality. –  Wyck Apr 26 '11 at 18:03

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Perfect! Thanks this is a much better way to do this. –  Wyck Apr 27 '11 at 21:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.