Hugh Lashbrooke

Product manager by day, tabletop game designer by night.

WordPress: Add items to the ‘At a glance’ widget

, ,

The WordPress dashboard includes a handy widget that displays some brief information about your site at a glance – rather creatively, it’s titled  ‘At a Glance’. By default it shows you the number of posts, pages and comments on your site allowing you to click through to edit each of those sections if you have the right permissions.

When building a plugin with a custom post type, it’s often a good idea to add your post type to the widget as it makes things more accessible and generally more user friendly for everyone. Using the snippet below you can add any number of custom post types to this widget (I’ve also included some useful CSS to customise the icons displayed):


View this gist on GitHub

All you need to do is replace the $post_types array with an array of the post types that you want to add to the widget. You’ll see that the snippet includes a security check so that only users with permission to edit each post type will be able to click through and do so.

The CSS will replace the circle with the same icon that the Posts post type uses. You can replace the content attribute there with one of the icons from this set – just select your icon and copy the relevant CSS from the link provided.

6 responses to “WordPress: Add items to the ‘At a glance’ widget”

  1. Steve Avatar
    Steve

    Any ideas as to how I can utilise the custom post types own icon that is used in the menu? It would be great to have the custom posts dashicon used in the At A Glance panel.

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      You’ll need to find out the value of the content property used in the CSS for that icon. If you inspect the menu icon in your browser look for the div with the wp-menu-image class and find the content property of the ::before section. If that’s all Greek to you, then ask a friend to help you out 🙂

      If the menu icon is a custom font, you’ll also have to add the font-family property with the correct value to the CSS from this example.

  2. Drew @ Recipe for Disaster Avatar
    Drew @ Recipe for Disaster

    Clean code Hugh! Very elegant solution. I found your post researching how to filter the page count on the ‘At a glance’ admin widget. Does core not include a filter hook to exclude posts from the counts? The closest I’ve found is the wp_count_posts filter, but it only returns an object with the count statuses, which doesn’t help me. Any suggestions?

    1. Drew @ Recipe for Disaster Avatar
      Drew @ Recipe for Disaster

      Figured it out! wp_count_posts was the right filter, I just wasn’t requesting all the filters parameters. Here’s a gist if anyone is interested: https://gist.github.com/toddsby/9276990

      1. Hugh Lashbrooke Avatar
        Hugh Lashbrooke

        Glad you found a solution. Thanks for posting the gist – always great to share these things with everyone 🙂

  3. CCDzine Avatar
    CCDzine

    I found this handy plugin in the WordPress repository that add the post types and allows you to select a Dashicon: https://wordpress.org/plugins/glance-that/

Leave a Reply

Your email address will not be published. Required fields are marked *