Creating conditional notices in the WordPress Admin

While working on a project today it occurred to me that my theme’s custom slideshow carousel categories lacked a reminder notice as to how they are used and updated.

The custom slide carousel

slideshow example screenshot.
This is just a screenshot, not the working gallery.
  • Posts are assigned to categories named “Carousel” or “Video Carousel”
  • The post’s Thumbnail / Featured Image is used as the slide’s image
  • The post’s excerpt is used as the slide’s caption
  • A WP_Query loops through the category name
  • Foundation 6’s Orbit provides the carousel framework
  • The front-page.php template displays the working slideshow

Naturally, I shouldn’t assume a user would instantly know what the Categories mean let alone how they display content to the homepage. As it turns out Conditional Tags can be used in the WordPress admin dashboard and added to my Child Theme‘s functions.php file.

The admin notice functions.php code

Screenshot from my post admin dashboard
<?php
// Place admin message about the Carousel Video and Carousel categories
function pz_carousel_admin_notice() {
	if ( is_admin() ) {
		if ( in_category('Carousel Video') ) {
			$class = 'video-carousel-notice notice notice-info is-dismissible';
			$message = __( 'Video Carousel posts are ordered by most recently updated to least. They display on the homepage', 'sample-text-domain' );
			printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 
		}

		if ( in_category('Carousel') ) {
			$class = 'slide-carousel-notice notice notice-info is-dismissible';
			$message = __( 'Carousel posts are ordered by most recently updated to least. They display on the homepage', 'sample-text-domain' );
			printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 
		}
	}

}
add_action( 'admin_notices', 'pz_carousel_admin_notice' );
?>

How it works

The function performs a boolean check if the currently displayed page is the WordPress Admin Screen. If that passes it’s followed by two boolean checks for categories named Carousel Video or Carousel. If either pass then the admin notice displays at the top of the WordPress post dashboard to remind the user the purpose of the category and how it works within the slideshow.

Putting it together

This isn’t a perfectly crafted solution but its better than adding yet another plugin to my Multisite. 😀