In search of a notice API

Yes, I’m writing about the admin notices. I know some are already up to their ears with that and I’m on that boat. The admin area is – and should be – for the user. Any notices that appear need to be informational. Not a nag. With that being said, I know there are some that would like to promote or even encourage their users to leave a review. I’m for that but there is a time and a place.

The admin area isn’t one.┬áNor should it be.

I do think part of that is because there really is no proper function, or hook, for using those. [ There really should not be. ] But for the sake of experimentation I went and played around with a few things. This is what ensued:

The code

It is pretty quick and decently documented.

// because otherwise it will be a turd-nugget
add_action( 'admin_init', 'check_for_notice_dismiss' );
function check_for_notice_dismiss() {
	// we check if the setting is set, making sure that it does not return false
	if ( get_theme_mod( 'theme-notice' ) === false ) {
		// we add our action for the notice
		add_action( 'admin_notices', 'we_add_our_notice' );
		// super hackish only because there are some that aren't running 4.5 and
		// so they can't use wp_add_inline_script()
		add_action( 'admin_footer', 'our_jquery_action' );
		add_action( 'wp_ajax_our_jquery_dismiss', 'our_jquery_dismiss' );

// callback for our admin notice
function we_add_our_notice() {
	// checking for our theme screen because we can
	if ( get_current_screen()->id !== 'themes' ){
		// get the message
		$string = __( 'How about you review our theme?', 'text-domain' );
		$html = '<div id="my-theme-notice" class="notice is-dismissible notice-info"><p>%s</p></div>';
		// the final outout of the notice
		printf( $html, $string );

// hackish job so we don't have to enqueue an extra file
function our_jquery_action(){ ?>
jQuery( document ).on( 'click', '#my-theme-notice .notice-dismiss', function() {
		url: ajaxurl,
		data: {
			action: 'our_jquery_dismiss'
<?php }

// callback function for our AJAX
function our_jquery_dismiss() {
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		set_theme_mod( 'theme-notice', true );

Really I only did test using a theme which does explain the use of get_theme_mod(). There is potential in creating some standard function in adding a dismissible notice.

Something along the lines of:

function wp_add_notice( $message, $dismiss = true, $type = '' ) {}

Or even as a class:

class WP_Notice {

	var dismiss = true;
	var types = array( 'info', 'error', 'updated', 'saved' );

	public function __construct( $message, $type, $dismiss ) {
		if ( !$message )
		// the rest of the code

	private function render_message( $message, $classes ) {
		printf( '<div class="%s"><p>%s</p></div>', esc_attr( $classes ), esc_html( $message ) );

I do think the class would be better suited for that but that could just be me. There you have it! Possible ideas to iterate over.

Published by


Born in El Salvador. Loves to read, write, draw, paint, build, test, typography, hike, photography, art, design, sewing, and many other things.