Skip to content
Code & Tools

Writing a Clean WordPress Shortcode

Theo Marsh
1 min read
568
Cover photo for “Writing a Clean WordPress Shortcode”

Shortcodes are old, but they are still the simplest way to give writers a reusable block in the classic editor.

Most of the improvement in a draft comes from cutting. We aim to remove a fifth of every piece on the second pass, and the reader never misses it.

shortcode-note.php
function quill_note_shortcode( $atts, $content = '' ) {
	$atts = shortcode_atts( array( 'type' => 'info' ), $atts, 'note' );

	ob_start();
	?>
	<aside class="note note--<?php echo esc_attr( $atts['type'] ); ?>">
		<?php echo wp_kses_post( $content ); ?>
	</aside>
	<?php
	return ob_get_clean();
}
add_shortcode( 'note', 'quill_note_shortcode' );
PHP UTF-8·LF·12 lines

Taking notes by hand is slower, and that is the point. The friction forces you to decide what matters before you write it down.

Reading work aloud is the cheapest editing tool there is. Your ear catches rhythm problems that your eye skips over, especially in long sentences.

4
Continue reading

Related posts

All posts
Conversation

Comments · 4

Posting as guest · Sign in
  1. Wren Holloway ·

    Do you have a recommendation for someone just starting out? Everything I find assumes years of experience.

  2. Kofi Mensah ·

    Such a clear explanation. I finally understand why our pages felt sluggish on mobile.

    1. Theo Marsh Author ·

      Glad it helped. The second pass is where it really clicks.

  3. Daniel Osei ·

    Small typo in the third paragraph, but otherwise a perfect read with my morning coffee.