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.

Hey, I've been trying to get a pending count to appear on the admin sidebar, for pending posts, like the little bubble that appears for pending comments:

Comments pending bubble

Offtopic: Am I the only one that thinks this should be core behaviour? Where should I suggest this feature?

Anyhow, I found this plugin, but I noticed it didn't always worked. Sometimes the notifier appears on Pages or other item.

The code it uses to add the pending count goes something like this:

$menu[5][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';

So, clearly the problem is the hard-coded 5 there, but how can I update it so it always points to Posts?

I'll be glad to commit this change to the plugin if we know the answer.

Thanks!

share|improve this question
    
$menu[5] is hard-coded as Posts in wp-admin/menu.php, at least in 3.0. When do you see the notification showing up in a different area? Can you narrow down the configuration where you see this happening? –  goldenapples Nov 11 '10 at 6:17
    
There is a specific filter available for ordering menu items , some plugins hook onto that filter and move items around, which in turn results in 'posts' not being key 5.. –  t31os Nov 11 '10 at 11:07
    
For anyone interested on this feature, I submitted a ticket to WP's Trac. Seems that it has been scheduled for a Future Release :) - core.trac.wordpress.org/ticket/15406 –  Ignacio Nov 14 '10 at 23:09

3 Answers 3

up vote 4 down vote accepted

@ign

Replace the line of code you posted with the following..

foreach( $menu as $menu_key => $menu_data ) :
    if( 'edit.php' != $menu_data[2] )
        continue;
    $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
endforeach;

..that should avoid the need to know the specific key.. (let me know if any problems)..

Hope that helps.. :)

share|improve this answer
    
This worked like a charm. Thanks, man! –  Ignacio Nov 11 '10 at 18:26
    
You're welcome... ;) And additional thanks to Somatic for the follow-up information .. –  t31os Nov 13 '10 at 11:37

as a followup to t31os' answer, here's the complete code needed (combining the contents of the plugin mentioned with t31os' fix), with modifications to handle custom post types as well:

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 = 0;
    if ( !empty($num_posts->$status) )
        $pending_count = $num_posts->$status;

    // build string to match in $menu array
    if ($type == 'post') {
        $menu_str = 'edit.php';
    } else {
        $menu_str = 'edit.php?post_type=' . $type;
    }

    // loop through $menu items, find match, add indicator
    foreach( $menu as $menu_key => $menu_data ) {
        if( $menu_str != $menu_data[2] )
            continue;
        $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
    }
    return $menu;
}

place this in functions.php, no need for the plugin.

share|improve this answer
    
Wow, thanks. I've already contacted the plugin creator in order to commit those changes in case anyone needs this behaviour. So, does anyone know why this isn't working like that by default? –  Ignacio Nov 11 '10 at 19:16
    
I hadn't even checked the plugin, thanks for following-up. The code you've provided makes for a nice simple function that other users can now copy if they wish to have this functionality.. nice work... ;) –  t31os Nov 13 '10 at 11:40
    
Q: Isn't the function setting the $type var, and in essence rendering the if ($type == 'post') { condition pointless? –  t31os Nov 20 '10 at 13:18
    
yes, this example is a little odd, as I adapted it from one where I'm polling all the post types in the system and running through the function (here I had to just declare the desired vars so the example would work standalone). The point of the conditional is to build the appropriate URL for everything other the the core post type... –  somatic Nov 22 '10 at 22:21
    
Very Nice mate... thanks a lot –  Sagive SEO Feb 15 '13 at 8:41

I made a slight alteration to somatic's post which allows for multiple post types:

// Add pending numbers to post types on admin menu
function show_pending_number($menu) {    
    $types = array("post", "page", "custom-post-type");
    $status = "pending";
    foreach($types as $type) {
        $num_posts = wp_count_posts($type, 'readable');
        $pending_count = 0;
        if (!empty($num_posts->$status)) $pending_count = $num_posts->$status;

        if ($type == 'post') {
            $menu_str = 'edit.php';
        } else {
            $menu_str = 'edit.php?post_type=' . $type;
        }

        foreach( $menu as $menu_key => $menu_data ) {
            if( $menu_str != $menu_data[2] )
                continue;
            $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
            }
        }
    return $menu;
}
add_filter('add_menu_classes', 'show_pending_number');
share|improve this answer
    
Very helpful! Thanks so much! –  Shawn Strickland Aug 20 '13 at 16:24

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.