Last active
February 1, 2021 18:12
-
-
Save papazetis/aa93c11ae1cf93657f4784a439830a37 to your computer and use it in GitHub Desktop.
Add Custom Post Types to "At a Glance"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add custom post types count action to WP Dashboard (At a Glance) | |
*/ | |
function add_cpt_glance_items( $items ) { | |
$args = array( | |
'public' => true, // Showing public post types only | |
'_builtin' => false // Except the build-in wp post types (page, post, attachments) | |
); | |
// Getting your custom post types | |
$post_types = get_post_types( $args, 'object', 'and' ); | |
foreach ( $post_types as $post_type ) { | |
// Counting each post | |
$num_posts = wp_count_posts( $post_type->name ); | |
// Number format | |
$num = number_format_i18n( $num_posts->publish ); | |
// Text format | |
$text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num ); | |
// Dashicon | |
$dashicon = ! empty( $post_type->menu_icon ) ? $post_type->menu_icon : 'dashicons-marker'; | |
// If use capable to edit the post type | |
if ( current_user_can( 'edit_posts' ) ) { | |
// Show with link | |
$item = '<i class="dashicons ' . $dashicon . '"></i><a class="' . $post_type->name . '-count" href="'.admin_url('edit.php?post_type=' . $post_type->name) . '">' . $num . ' ' . $text . '</a>'; | |
} else { | |
// Show without link | |
$item = '<i class="dashicons ' . $dashicon . '"></i><span class="' . $post_type->name . '-count">' . $num . ' ' . $text . '</span>'; | |
} | |
// Save in array | |
$items[] = $item; | |
} | |
// return them | |
return $items; | |
} | |
add_filter( 'dashboard_glance_items', 'add_cpt_glance_items', 10, 1 ); | |
add_action( 'admin_head', function () { | |
?> | |
<style> | |
#dashboard_right_now li a:before, | |
#dashboard_right_now li > span:before { | |
content: none; /* remove generic icon for items added by CPTs ? */ | |
} | |
#dashboard_right_now li > i { | |
color: #606a73; | |
speak: none; | |
display: inline-block; | |
position: relative; | |
padding: 0 5px 0 0; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
text-decoration: none !important; | |
vertical-align: top; | |
} | |
</style> | |
<?php | |
}); | |
// Add custom post types count action to WP Dashboard (At a Glance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment