Add your custom post type count to Right Now admin dashboard widget with WordPress plugin

Wordpress Logo BlueRecently I've published my latest WordPress plugin Quick Poll where I've used custom post type feature of recent WordPress versions to implement voting poll functionality. Wouldn't it be nice to show your custom post type count on WordPress dashboard Right Now widget right bellow the number of posts, pages, comments, categories and tags? This would definitely help your custom post type to blend into default WordPress functionality. It is also handy because clicking on your custom post type count will take user right to the list of your custom post type posts. In this article I'm presenting code to do just that.

Before we start here's my WordPress admin dashboard Right Now widget screenshot before we add my custom post type count:

WordPress Admin Dashboard Right Now widget before

And now here's the code to add my custom post type with slug 'quick_poll' to admin dashboard Right Now widget with links for users with 'edit_posts' capability. For this code to work we are using right_now_content_table_end hook located in wp-admin/includes/dashboard.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Here I define my custom post type slug
define('QUICK_POLL_POST_TYPE', 'quick_poll');
 
function quick_poll_right_now() {
    // Count number of voting polls
    $num_polls = wp_count_posts(QUICK_POLL_POST_TYPE);
 
    // i18n format published voting polls count
    $num = number_format_i18n($num_polls->publish);
 
    // i18n format label noun for plural or singular
    $text = _n( 'Poll', 'Polls', intval($num_polls->publish));
 
    // Add links to poll list only if user has 'edit_posts'
    // capability, else add just text with no links
    if (current_user_can('edit_posts')) {
        $num = '<a href="edit.php?post_type='.QUICK_POLL_POST_TYPE.'">'.$num.'</a>';
        $text = '<a href="edit.php?post_type='.QUICK_POLL_POST_TYPE.'">'.$text.'</a>';
    }
 
    // Echo table row start, table cells and table row end
    echo '<tr>';
    echo '<td class="first b b-posts">'.$num.'</td>';
    echo '<td class="t posts">'.$text .'</td>';
    echo '</tr>';
}
add_action('right_now_content_table_end', 'quick_poll_right_now');

Since I believe this code comments give enough explanation I won't bother you with additional explanations. Ill just place my WordPress admin dashboard Right Now widget screenshot after our modifications:

WordPress Admin Dashboard Right Now widget after

If something is unclear you can leave your comment and someone will assist as soon as possible. Have fun!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

One thought on “Add your custom post type count to Right Now admin dashboard widget with WordPress plugin

Leave a Reply to Everaldo Cancel reply

Your email address will not be published. Required fields are marked *