WordPress 4.7’s do_shortcode_tag filter

For the last few months I’ve been using a modified version of “Better Click to Tweet“, a WordPress plugin by The WP Steward. It implements a simple shortcode that enables “Click to Tweet” functionality on a piece of text in your page or post. The problem was, I wanted to append content to the output of the shortcode each time it was used. I’d looked at the plugin source, and there were no filters that I could implement do this. So my solution was to modify the plugin’s source and use my forked version on my sites.

Anyone who’s done this knows what’s coming next…. a new version of the plugin came out. Oh bother, now I need to download the new version and re-apply my changes.

TFW when you had to modify a plugin and now you need to redo your changes before upgrading…. without docs. Time to diff the original… — Shawn Hooper (@ShawnHooper) January 11, 2017

This time around, I thought… “There has to be a better way!” and dug into WordPress’ core code. Lo and behold – WordPress 4.7 has a new filter that helps me do exactly what I needed to!

Enter: do_shortcode_tag

The new do_shortcode_tag filter allows you to modify the output of a shortcode before it gets mixed in with a post’s content. Beautiful!

The filter gives you access to the HTML that’d been generated by the shortcode, the name of the shortcode that was run, along with all the attributes (if any) that were passed to the shortcode in your post’s content.

NOTE: The 4th attribute is the regular expression match array produced when processing shortcodes. I haven’t played with this enough to know where it would be handy.

Solving my initial problem…

In order to append the HTML to the end of the shortcode’s output, I added the following code. I did this in my theme’s functions.php as it applied directly to the visual style of the site. If you were adding functionality, I would recommend doing this in a plugin.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /** * Append a div with the tweetbar class * after the output of any [bctt] shortcode * * @param string $output The output from the shortcode * @param string $tag The name of the shortcode * * @return string The modified output */ function append_tweetbar_to_bctt ( $output , $tag ) { if ( 'bctt' !== $tag ) { return $output ; } return $output . '<div class="tweetbar"></div>' ; } add_filter ( 'do_shortcode_tag' , 'append_tweetbar_to_bctt' , 10 , 2 ) ; /** * Append a div with the tweetbar class * after the output of any [bctt] shortcode * * @param string $output The output from the shortcode * @param string $tag The name of the shortcode * * @return string The modified output */ function append_tweetbar_to_bctt( $output, $tag ) { if ( 'bctt' !== $tag ) { return $output; } return $output . '<div class="tweetbar"></div>'; } add_filter('do_shortcode_tag', 'append_tweetbar_to_bctt', 10, 2);

As I didn’t need to use the $atts or $m arguments, I only included the first two parameters in my filter ($output and $tag). The first thing I did was check to see if I was processing the Better Click to Tweet (“bctt”) shortcode. If I was, then I appended the code I wanted, and returned it to be included in the post’s content.

What’s your favourite WordPress filter?