The dashboard_glance_items
hook is a WordPress action hook that is called to add or remove items from the "At a Glance" dashboard widget.
The "At a Glance" widget provides a quick summary of the site’s content, such as the number of posts, pages, comments, and categories. By using the dashboard_glance_items
hook, developers can customize the widget by adding their own summary items or removing existing ones.
This hook takes no arguments and is called within the wp_dashboard_setup()
function.
Example usage code:
// Add a custom summary item to the "At a Glance" widget
function custom_glance_items() {
$post_types = get_post_types( array( 'public' => true ) );
foreach ( $post_types as $post_type ) {
$num_posts = wp_count_posts( $post_type );
$num = number_format_i18n( $num_posts->publish );
$text = _n( '%s ' . $post_type, '%s ' . $post_type . 's', intval($num_posts->publish) );
if ( current_user_can( 'edit_posts' ) ) {
$output = '<a href="edit.php?post_type=' . $post_type . '">' . $num . ' ' . $text . '</a>';
}
echo '<li class="' . $post_type . '-count">' . $output . '</li>';
}
}
add_action( 'dashboard_glance_items', 'custom_glance_items' );
In this example, the custom_glance_items()
function is defined to add a custom summary item to the "At a Glance" widget. It counts the number of published posts for each public post type and displays the count along with a link to edit those posts. This function is then attached to the dashboard_glance_items
hook using the add_action()
function, ensuring that it is called when the dashboard widget is loaded.