WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Dashboard - At a Glance - Custom Post Types (7 posts)

  1. dotkev
    Member
    Posted 7 months ago #

    A popular hack is to add custom post types to the dashboard "Right Now" or now "At a Glance." The codex also has a similar example.

    Looks like the new hook is "dashboard_glance_items". This works, but I'd love a way to add the defined CPT icon instead of a generic page, post, or comment icon. Also, since the icon is styled from .post-count a:before, I can't add an additional link on the item to view only pending items.

    Here is my first pass at adding CPTs to the "At a Glance" section (screenshot):

    // Add custom taxonomies and custom post types counts to dashboard
    add_action( 'dashboard_glance_items', 'my_add_cpt_to_dashboard' );
    
    function my_add_cpt_to_dashboard() {
      $showTaxonomies = 1;
      // Custom taxonomies counts
      if ($showTaxonomies) {
        $taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' );
        foreach ( $taxonomies as $taxonomy ) {
          $num_terms  = wp_count_terms( $taxonomy->name );
          $num = number_format_i18n( $num_terms );
          $text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, $num_terms );
          $associated_post_type = $taxonomy->object_type;
          if ( current_user_can( 'manage_categories' ) ) {
            $output = '<a href="edit-tags.php?taxonomy=' . $taxonomy->name . '&post_type=' . $associated_post_type[0] . '">' . $num . ' ' . $text .'</a>';
          }
          echo '<li class="taxonomy-count">' . $output . ' </li>';
        }
      }
      // Custom post types counts
      $post_types = get_post_types( array( '_builtin' => false ), 'objects' );
      foreach ( $post_types as $post_type ) {
        if($post_type->show_in_menu==false) {
          continue;
        }
        $num_posts = wp_count_posts( $post_type->name );
        $num = number_format_i18n( $num_posts->publish );
        $text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
        if ( current_user_can( 'edit_posts' ) ) {
            $output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a>';
        }
        // pending items count
        // if ( $num_posts->pending > 0 ) {
        //     $num = number_format_i18n( $num_posts->pending );
        //     $text = _n( $post_type->labels->singular_name . ' pending', $post_type->labels->name . ' pending', $num_posts->pending );
        //     if ( current_user_can( 'edit_posts' ) ) {
        //         $output .= '<a class="waiting" href="edit.php?post_status=pending&post_type=' . $post_type->name . '">' . $num . ' pending</a>';
        //     }
        // }
        echo '<li class="page-count ' . $post_type->name . '-count">' . $output . '</td>';
      }
    }
  2. dotkev - You can do this.

    So I have my CPTs like this: http://halfelf.org/2012/my-custom-posttypes-live-in-mu/

    and then I style them in the sidebar AND menu like this:

    function helf_cpts_css() {
           echo "<style type='text/css'>
                   #adminmenu #menu-posts-ebooks div.wp-menu-image:before, #dash-right-now li.ebooks-count a:before {
                        content: '\\f118';
                        margin-left: -1px;
                    }
                   #adminmenu #menu-posts-plugins div.wp-menu-image:before, #dash-right-now li.plugins-count a:before {
                        content: '\\f237';
                        margin-left: -1px;
                    }
                 </style>";
        }
    
        add_action('admin_head', 'helf_cpts_css');

    Now they all look the same :)

  3. dotkev
    Member
    Posted 7 months ago #

    Thanks, great solution!

    Instead, I'm overriding the css to not display the font icon at all and instead put the $post_type->menu_icon inline, since all of my cpts already have defined icons.

  4. Marj Wyatt
    Member
    Posted 7 months ago #

    I bumped into this issue last night. In my case, I had my right now code to sift through all custom post types and taxonomies. Because the solutions offered here required "hard coding" the CPT data, I went to the drawing board to re-work my version of the code.

    My initial code was based on this snippet:
    http://wpsnipp.com/index.php/functions-php/include-custom-post-types-in-right-now-admin-dashboard-widget/

    This is the revised code:

    <?php
    // Add Custom Post Type to WP-ADMIN Right Now Widget
    // Ref Link: http://wpsnipp.com/index.php/functions-php/include-custom-post-types-in-right-now-admin-dashboard-widget/
    // http://wordpress.org/support/topic/dashboard-at-a-glance-custom-post-types
    // http://halfelf.org/2012/my-custom-posttypes-live-in-mu/
    function vm_right_now_content_table_end() {
        $args = array(
            'public' => true ,
            '_builtin' => false
        );
        $output = 'object';
        $operator = 'and';
        $post_types = get_post_types( $args , $output , $operator );
        foreach( $post_types as $post_type ) {
            $num_posts = wp_count_posts( $post_type->name );
            $num = number_format_i18n( $num_posts->publish );
            $text = _n( $post_type->labels->name, $post_type->labels->name , intval( $num_posts->publish ) );
            if ( current_user_can( 'edit_posts' ) ) {
                $cpt_name = $post_type->name;
            }
            echo '<li class="post-count"><tr><a href="edit.php?post_type='.$cpt_name.'"><td class="first b b-' . $post_type->name . '"></td>' . $num . '&nbsp;<td class="t ' . $post_type->name . '">' . $text . '</td></a></tr></li>';
        }
        $taxonomies = get_taxonomies( $args , $output , $operator );
        foreach( $taxonomies as $taxonomy ) {
            $num_terms  = wp_count_terms( $taxonomy->name );
            $num = number_format_i18n( $num_terms );
            $text = _n( $taxonomy->labels->name, $taxonomy->labels->name , intval( $num_terms ));
            if ( current_user_can( 'manage_categories' ) ) {
                $cpt_tax = $taxonomy->name;
            }
            echo '<li class="post-count"><tr><a href="edit-tags.php?taxonomy='.$cpt_tax.'"><td class="first b b-' . $taxonomy->name . '"></td>' . $num . '&nbsp;<td class="t ' . $taxonomy->name . '">' . $text . '</td></a></tr></li>';
        }
    }
    add_action( 'dashboard_glance_items' , 'vm_right_now_content_table_end' );
    ?>

    The same code is up at Pastbin:
    http://pastebin.com/Jc00GNWV

    My code does not place custom icons. It leverages those built into WordPress for posts. But it does allow for any custom post type or taxonomy one might imagine.

    Like Ipstenu, I put all code related to CPTs into mu-plugins so a theme change won't break the code.

    Thanks for giving me a direction though!

  5. UaMV
    Member
    Posted 6 months ago #

    Hello, folks! After finding this thread, I pulled together a plugin, Glance That, to add this functionality to your dashboard. Hope it helps!

  6. dotkev
    Member
    Posted 4 months ago #

    Nice work everyone!

    @Marj Wyatt, I made just a few MINOR edits to your code to pull in the cpt singular name and a cpt icon class for styling.

    @ UaMV Member, I'd love to contribute to your plugin. I need an 8th day. :-)

    // Add Custom Post Type to WP-ADMIN Right Now Widget
    // Ref Link: http://wpsnipp.com/index.php/functions-php/include-custom-post-types-in-right-now-admin-dashboard-widget/
    // http://wordpress.org/support/topic/dashboard-at-a-glance-custom-post-types
    // http://halfelf.org/2012/my-custom-posttypes-live-in-mu/
    function vm_right_now_content_table_end() {
        $args = array(
            'public' => true ,
            '_builtin' => false
        );
        $output = 'object';
        $operator = 'and';
        $post_types = get_post_types( $args , $output , $operator );
        foreach( $post_types as $post_type ) {
            $num_posts = wp_count_posts( $post_type->name );
            $num = number_format_i18n( $num_posts->publish );
            $text = _n( $post_type->labels->singular_name, $post_type->labels->name , intval( $num_posts->publish ) );
            if ( current_user_can( 'edit_posts' ) ) {
                $cpt_name = $post_type->name;
            }
            echo '<li class="'.$cpt_name.'-count"><tr><a href="edit.php?post_type='.$cpt_name.'"><td class="first b b-' . $post_type->name . '"></td>' . $num . ' <td class="t ' . $post_type->name . '">' . $text . '</td></a></tr></li>';
        }
        $taxonomies = get_taxonomies( $args , $output , $operator );
        foreach( $taxonomies as $taxonomy ) {
            $num_terms  = wp_count_terms( $taxonomy->name );
            $num = number_format_i18n( $num_terms );
            $text = _n( $taxonomy->labels->name, $taxonomy->labels->name , intval( $num_terms ));
            if ( current_user_can( 'manage_categories' ) ) {
                $cpt_tax = $taxonomy->name;
            }
            echo '<li class="post-count"><tr><a href="edit-tags.php?taxonomy='.$cpt_tax.'"><td class="first b b-' . $taxonomy->name . '"></td>' . $num . ' <td class="t ' . $taxonomy->name . '">' . $text . '</td></a></tr></li>';
        }
    }
    add_action( 'dashboard_glance_items' , 'vm_right_now_content_table_end' );
  7. Marj Wyatt
    Member
    Posted 4 months ago #

    @dotkev ... glad my snippet helped you out!

Reply

You must log in to post.

About this Topic