AI Verified

Name

MX · Add All Post Types to Dashboard "At a Glance"

About

Add all post_types found in the database to the "At a Glance" dashboard widget

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

cmccann

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.

History

Last modified:

09/02/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:

-------------------------------------------------------------------------------------------------------------
Name | Potential vulnerability found : Cross Site Scripting
Line | 35
Code | printf( $text, number_format_i18n( $count )
Code | Line | 35 : $text = _n( '%s ' . $type_object->labels->singular_name, '%s ' . $type_object->labels->name, $count, 'your_textdomain' ));

Name | Potential vulnerability found : Cross Site Scripting
Line | 36
Code | printf('<a class="%1$s-count" href="edit.php?post_type=%1$s" title="%1$s">%2$s</a>', $type, $text)
Code | Line | 17 : $post_types = []);

Name | Potential vulnerability found : Cross Site Scripting
Line | 36
Code | printf('<a class="%1$s-count" href="edit.php?post_type=%1$s" title="%1$s">%2$s</a>', $type, $text)
Code | Line | 35 : $text = _n( '%s ' . $type_object->labels->singular_name, '%s ' . $type_object->labels->name, $count, 'your_textdomain' ));

Name | Potential vulnerability found : Cross Site Scripting
Line | 35
Code | printf( $text, number_format_i18n( $count )
Code | Line | 35 : $text = _n( '%s ' . $type_object->labels->singular_name, '%s ' . $type_object->labels->name, $count, 'your_textdomain' ));

Name | Potential vulnerability found : Cross Site Scripting
Line | 44
Code | printf('<span class="%1$s-count">%2$s</span>', $type, $text)
Code | Line | 17 : $post_types = []);

Name | Potential vulnerability found : Cross Site Scripting
Line | 44
Code | printf('<span class="%1$s-count">%2$s</span>', $type, $text)
Code | Line | 35 : $text = _n( '%s ' . $type_object->labels->singular_name, '%s ' . $type_object->labels->name, $count, 'your_textdomain' ));

Found 6 vulnerabilities

MX · Add All Post Types to Dashboard "At a Glance"

 
                    
1if ( ! function_exists('mx_custom_glance_items'))
2{
3 function mx_custom_glance_items()
4 {
5 // Return a list of all saved post_type entries in the database
6 global $wpdb;
7 $post_types_result = $wpdb->get_results("
8 SELECT
9 DISTINCT(post_type),
10 COUNT(*) as count
11 FROM {$wpdb->posts}
12 GROUP BY post_type
13 ORDER BY post_type ASC
14 ", ARRAY_A);
15 
16 // Loop through DB result and save type and count for each
17 $post_types = [];
18 foreach ($post_types_result as $arr)
19 {
20 if ( ! in_array($arr['post_type'], ['post', 'comment', 'page']))
21 {
22 $post_types[$arr['post_type']] = $arr['count'];
23 }
24 }
25 arsort($post_types);
26 
27 foreach ($post_types as $type => $count)
28 {
29 // Attempt to find a registered post type of this name;
30 $type_object = get_post_type_object($type);
31 
32 // Did you find a fully registered post type? If so, create a basic admin edit link and display in human-readable form
33 if ( ! is_null($type_object))
34 {
35 $text = _n( '%s ' . $type_object->labels->singular_name, '%s ' . $type_object->labels->name, $count, 'your_textdomain' );
36 $text = sprintf( $text, number_format_i18n( $count ) );
37 $items[] = sprintf('<a class="%1$s-count" href="edit.php?post_type=%1$s" title="%1$s">%2$s</a>', $type, $text) . "\n";
38 }
39 
40 // No registered type; don't create a link and just display the post_type key
41 else
42 {
43 $text = _n( '%s ' . $type, '%s ' . $type, $count, 'your_textdomain' );
44 $text = sprintf( $text, number_format_i18n( $count ) );
45 $items[] = sprintf('<span class="%1$s-count">%2$s</span>', $type, $text) . "\n";
46 }
47 }
48 
49 // Send back our generated list
50 return $items;
51 }
52 add_filter('dashboard_glance_items', 'mx_custom_glance_items', 10, 1);
53}

0

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

0

Hide WP Block Editor on specific post type

Added: 1 year ago

Last Updated: 1 year ago

WordPress Admin

AI Verified

0

Display Database Name on Admin Bar

Added: 4 months ago

Last Updated: 4 months ago

This snippet will display your WordPress database name on your admin bar for administrator users.

WordPress Admin

AI Verified

0

Disable specific file type upload

Added: 1 year ago

Last Updated: 1 year ago

Diasbale specific file type upload

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

0

MX · Add All Post Types to Dashboard "At a Glance"

Added: 1 year ago

Last Updated: 1 year ago

Add all post_types found in the database to the "At a Glance" dashboard widget