Shortcodes are a great way to extend WordPress and as developers there are some pretty groovy undocumented tricks we can employ.
Shortcode Taglets
Example: [term="ionic-anklets"]
Sometimes it makes sense for a shortcode to have a single parameter and would be improved syntactically by not explicitly setting parameter names. The example above is for a plugin for inserting terms for a constantly-changing subject for which only one parameter will ever be relevant. You could omit the quotes, but then you couldn’t include spaces in the value.
This will trigger the term
shortcode and atts
will looks like array( 0 => '="test"' )
. It’s interesting to note that the “=” and quotes are included in the value so your plugin will have to strip these to expose a usable value.
Shortcode Flags
Example: [inc 42]
A more common use-case is a shortcode attribute without a value. The example above is for a plugin which includes the content of another post; the shortcode is called inc
and the param 42
refers to the ID of the post to be included.
If the shortcode only expects a single param (such as the example above) $atts[0]
will contain the “flag”, but if multiple flags are expected you’ll have to iterate through $atts
and grab any param with an integer for a key.
Register with Closures
Shortcodes can be registered with Closures rather than by referencing a function and IMHO it leads to cleaner code. Example:
add_shortcode( 'inc', function( $atts, $content = null ) { foreach ( $atts as $att => $val ) echo $att.':'.$val.'<br>'; } );
Shortcode in Theme Template
Finally, using a shortcode in a theme template is as simple as:
<?=do_shortcode('[shortcode]');?>
Conclusion
These techniques are used quite frequently in my plugins, but as aforementioned, they are neither documented nor supported in WordPress Core so weigh that knowledge before using them yourself.
That said, there are a few aspects of WordPress and what Automattic “recommends” that I don’t personally agree with (deploring short echo tags or demanding support for PHP 5.2.4, for example) but that’s a rant for another article!