WP Admin Notes – Quick Bit of Code

I just added a feature to one of my sites that allows me to write a “note to the admin” (me) that doesn’t show up on the site. Here’s a look at one of the widgets in my admin area:

Wp trr notes

I really don’t want a title for that widget, but I also don’t want to have to open every text widget to remember what it is (many of my text widgets are without titles). By creating that shortcode I can surround any bits of text that is a note to myself, but it won’t appear on the site.

Creating a shortcode is really easy — and this one was especially easy because nothing is really happening. WP passes me the text that’s surrounded by the shortcode and I don’t do anything with it — just pass WP back an empty string.

I put this code inside the functions.php of my current theme:

function trr_note( $atts, $content = null ) {
	return '';
}
add_shortcode( 'trr_note', 'trr_note' );

Because of the way shortcodes work I can also call it like this:

[trr_note foo="This is my note for my eyes only!"]

The variable named foo can be named anything — since I’m doing nothing with that it doesn’t matter what it’s called.

The only other thing I had to do was tell WP to process shortcodes inside widget titles and text — apparently that’s not standard (which seems weird to me). I added these lines of code to the functions.php file:

add_filter('widget_text', 'do_shortcode');
add_filter('widget_title', 'do_shortcode');

It’s not rocket science, but it makes my admin duties easier since I can tell at a glance what specific widgets are for. I can also use it to “comment out” text on pages that I want to save for later.

Leave a Reply

Your email address will not be published. Required fields are marked *