AI Verified

Name

Add custom post types to the At a Glance Dashboard widget

About

This snippet adds your custom post types to the "At a Glance" widget on your WordPress Dashboard. By default, only Posts and Pages display, but this snippet will allow you to pull in counters for all of your CPTs.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

• "Only run in administration area" in the snippet settings. • You will need to replace "custom-post-type-x" with the slug of your custom post type.

Codevault

Jamie

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.jdab.co.uk

History

Last modified:

30/11/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 18
Code : printf( '<span class="page-count">%1$s</span>', esc_html( $text )
Vulnerable line : 13
Code : $text = _n( $cpt_info->labels->singular_name, $cpt_info->labels->name, $num )); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural

Found 1 vulnerabilities

Add custom post types to the At a Glance Dashboard widget

 
                    
1// Add custom post types to the At a Glance Dashboard widget
2function ataglance_dashboard_items( $items ) {
3 
4// Set the array of post types to display.
5$post_types = array( 'custom-post-type-1','custom-post-type-2','custom-post-type-3');
6 
7foreach ( $post_types as $cpt ) {
8 $cpt_info = get_post_type_object( $cpt );
9 $num_posts = wp_count_posts( $cpt );
10 $num = absint( $num_posts->publish + $num_posts->private );
11 
12 if ( $num ) {
13 $text = _n( $cpt_info->labels->singular_name, $cpt_info->labels->name, $num ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
14 $num = number_format_i18n( $num );
15 
16 if ( current_user_can( 'edit_posts' ) ) {
17 $text = '<a class="page-count ' . esc_attr( $cpt_info->name ) . '-count" href="edit.php?post_type=' . esc_attr( $cpt ) . '">' . $num . ' ' . esc_html( $text ) . '</a>';
18 } else {
19 $text = sprintf( '<span class="page-count">%1$s</span>', esc_html( $text ) );
20 }
21 
22 $items[] = $text;
23 }
24}
25return $items;
26}
27add_filter( 'dashboard_glance_items', 'ataglance_dashboard_items', 1 );

1

Comments

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

0

Hiding "Show Toolbar when viewing site" checkbox on profile page for specific user roles

Added: 1 year ago

Last Updated: 1 year ago

Have a user role that you want to hide "Show Toolbar when viewing site" checkbox on the profile page? This will remove the checkbox on specific user roles so Admins can control who has the admin toolb...

WordPress Admin

AI Verified

0

Hide admin notices for all users except 1

Added: 1 year ago

Last Updated: 1 year ago

Hide admin notices for all users except 1, defined by user ID

WordPress Admin

AI Verified

0

Move 'ID' column on Snippets menu

Added: 1 month ago

Last Updated: 1 month ago

Changes the position of the 'ID' column in the Snippets table.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

1

Add custom post types to the At a Glance Dashboard widget

Added: 10 months ago

Last Updated: 9 months ago

This snippet adds your custom post types to the "At a Glance" widget on your WordPress Dashboard. By default, only Posts and Pages display, but this snippet will allow you to pull in counters for all...

WordPress Admin

AI Verified

0

Add icons to the custom post types in the At a Glance Dashboard widget

Added: 10 months ago

Last Updated: 9 months ago

This snippet adds custom icons to your custom post type counters in the At a Glance widgets on your WordPress Dashboard. I have a snippet in my Codevault on how to add CPTs to this widget too.

Divi Builder

AI Verified

0

Enable the Dynamic Content option in the Divi Number Counter module

Added: 10 months ago

Last Updated: 9 months ago

This snippet enables Dynamic Content in the number field of Divi's Number Counter module. This would then allow you to control the content through a plugin such as ACF rather than the Divi Builder.