Categories
How To

Show Feedback in “Right Now”

Adding Jetpack messages to At a Glance in WordPress isn’t all that complicated, and it works out nicely.

The “Right Now” section of the WordPress dashboard is a great way to get an overview of the goings on of your site. But it doesn’t quite list everything. What if you could add things like ‘messages’ to the the “At a Glance” section like this:

The "At a Glance" section, with messages added in.

Guess what? You can!

The Code

Presmuing you’re using Jetpack’s contact form module, you automatically get a new kind of post called ‘Feedback.’ In order to make it display it’s count in “At a Glance,” there are two parts. First we add the CSS, which does the styling. Then we add the PHP that counts and displays the number of posts in feedback.

add_action( 'dashboard_glance_items', 'helf_dashboard_glance' );
add_action( 'admin_head', 'helf_dashboard_glance_css' );

/*
 * Show Feedback in "Right Now"
 */
function helf_dashboard_glance() {
	if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'contact-form' ) ) {
		foreach ( array( 'feedback' ) as $post_type ) {
			$num_posts   = wp_count_posts( $post_type );
			$count_posts = ( isset( $num_posts->publish ) ) ? $num_posts->publish : '0';
			if ( 0 !== $count_posts ) {
				if ( 'feedback' === $post_type ) {
					// translators: %s is the number of messages
					$text = _n( '%s Message', '%s Messages', $count_posts );
				}
				$text = sprintf( $text, number_format_i18n( $count_posts ) );
				printf( '<li class="%1$s-count"><a href="edit.php?post_type=%1$s">%2$s</a></li>', esc_attr( $post_type ), wp_kses_post( $text ) );
			}
		}
	}
}

/*
 * Custom Icon for Feedback in "Right Now"
 */
function helf_dashboard_glance_css() {
	if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'contact-form' ) ) {
		?>
		<style type='text/css'>
			#adminmenu #menu-posts-feedback div.wp-menu-image:before, #dashboard_right_now li.feedback-count a:before {
				content: '\f466';
				margin-left: -1px;
			}
		</style>
		<?php
	}
}