Home / dashboard / Add wordpress post types to the dashboard ‘At a glance’ widget
  • How to Add wordpress post types to the dashboard ‘At a glance’ widget

    add-wordpress-post-types-dashboard-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. 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.

    Once you have added the post types to the widget, they will have a circle icon next to them by default, but you can easily replace that with any of the dashicons available in MP6 and WordPress 3.8+. All this takes is some simple CSS - the CSS provided here 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.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );
    function custom_glance_items( $items = array() ) {
        $post_types = array( 'post_type_1', 'post_type_2' );
        foreach( $post_types as $type ) {
            if( ! post_type_exists( $type ) ) continue;
            $num_posts = wp_count_posts( $type );
            if( $num_posts ) {
                $published = intval( $num_posts->publish );
                $post_type = get_post_type_object( $type );
                $text = _n( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $published, 'your_textdomain' );
                $text = sprintf( $text, number_format_i18n( $published ) );
                if ( current_user_can( $post_type->cap->edit_posts ) ) {
                    $items[] = sprintf( '%2$s', $type, $text ) . "\n";
                } else {
                    $items[] = sprintf( '<span class="%1$s-count">%2$s</span>', $type, $text ) . "\n";
                }
            }
        }
        return $items;
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    #dashboard_right_now a.post_type-count:before,
    #dashboard_right_now span.post_type-count:before {
      content: "\f109";
    }
    
  • Josh Lobe

    Excellent snippet :)

    One suggestion for improvement. You can
    make the text displayed in the ‘at a glance’ widget link to the page
    where the custom post types are displayed; just like normal posts or
    pages.

    Simply change (first occurence):

    $items[] = sprintf( ‘%2$s’, $type, $text ) . “n”;

    To this:
    $items[] = sprintf( ‘%2$s’, $type, ‘<a href=”‘ . admin_url() . ‘edit.php?post_type=’ . $type . ‘”>’ . $text . ‘‘ ) . “n”;

    It will even work within the foreach loop; still allowing you to define the custom post types in the first variable of the function.

    Thanks man!!

    • razgriz

      I think you forgot to close your tag…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…

  • edizzz

    the icon no work…