Sometimes we want to add items to the dashboard widget called “At a Glance”. This can be done with the filter dashboard_glance_items.
add_filter( 'dashboard_glance_items', 'add_custom_glance_items', 10, 1 );
function add_custom_glance_items( $items = array() ) {
$items[] = '<a href="URL"'>My URL</a>';
return $items;
}
In the example above we use filter to add one item to list. Currently, it is added without HTML tag li. The place in the widget where items will be displayed can be changed with changing the priority of the function (3rd parameter of function add_filter).
Example:
This can be used to display the count of published custom post types in the “At a Glance” widget.
$num_posts = wp_count_posts( <enter post type here> );
if( $num_posts ) {
$published = intval( $num_posts->publish );
$items[] = sprintf( '<a class="%1$s-count" href="edit.php?post_type=%1$s"><your post title></a>', <enter post type here> );
}
If we put the example above in the function add_custom_glance_items, we can add the count of published posts for this post type. First, we get the count of the post by post type (only published). And after that using sprintf we generate a link, which can be added in the items list.
Another situation where this functionality can be used is when we want to display the number of users on a website. This can be done with the function get_users.
Usage of WordPress “At a Glance” widget can vary based on our needs, but its main purpose is to display statistical data.
Leave A Comment