4

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!

3
  • $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? Commented Nov 11, 2010 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
    Commented Nov 11, 2010 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
    Commented Nov 14, 2010 at 23:09

3 Answers 3

4

@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.. :)

1
  • You're welcome... ;) And additional thanks to Somatic for the follow-up information ..
    – t31os
    Commented Nov 13, 2010 at 11:37
7

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.

4
  • 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
    Commented Nov 11, 2010 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
    Commented Nov 13, 2010 at 11:40
  • Q: Isn't the function setting the $type var, and in essence rendering the if ($type == 'post') { condition pointless?
    – t31os
    Commented Nov 20, 2010 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
    Commented Nov 22, 2010 at 22:21
2

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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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