Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wp-mail-logging domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /mnt/serverpilot1_srv/shawnhooper/apps/shawn-hooper/public/wp-includes/functions.php on line 6114
WordPress 4.7's do_shortcode_tag filter - Shawn Hooper Skip to content

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.

https://twitter.com/ShawnHooper/status/819296122682413056

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 definition of the do_shortcode_tag filter in WordPress

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.

/**
 * 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 . '
'; } 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?

2 Comments

  1. great useful tips:) solve my issue that modify the original view file of the plugin.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.