I’m no fan of WordPress themes adding shortcodes. I’ve been pretty adamant about it in the past. But, all rules have exceptions, right?
So, you’re telling me that you — the guy who’s been drilling it into my head to not add theme shortcodes — that it might now be okay?
Well, yes. Technically, I’ve always been okay with themes adding shortcodes as long as those shortcodes are not intended to be used in the post content.
Shortcodes can be used for any number of purposes. Generally speaking, developers create shortcodes to be used in the post content. For this reason alone, shortcodes belong in a plugin. However, if you’re creating a shortcode to be used in other places where data portability is not an issue, go wild.
Here’s a few examples of what I consider exceptions to the rule:
- Shortcodes for use in theme templates.
- Shortcodes for use in theme settings, such as the footer text.
- Shortcodes for use in widgets (this is probably a bit of a gray area to some).
The problem is that users can still use these shortcodes in the post content even if it wasn’t your intention to allow them to do so. This discussion came up in the comments on the proposed WordPress 3.5 theme guideline revisions. This tutorial will show you how to avoid that scenario.
Adding shortcodes
I’m going to assume you’re already familiar with the process of adding shortcodes using WordPress’ add_shortcode() function, so I won’t cover that.
The following is an example of a few theme-defined shortcodes and how they should be set up.
add_action( 'init', 'my_add_shortcodes' );
function my_add_shortcodes() {
add_shortcode( 'shortcode_1', 'my_shortcode_1' );
add_shortcode( 'shortcode_2', 'my_shortcode_2' );
add_shortcode( 'shortcode_3', 'my_shortcode_3' );
}
This is just to give you a base to work with for the remainder of this tutorial. You can set up your own shortcodes however you like.
Not allowing theme shortcodes in the post content
We don’t want our shortcodes to be used at all in the post content. The best option here is to simply remove them when the the_content filter hook is called. WordPress makes this extremely easy with the remove_shortcode() function.
The following code will remove the three shortcodes we added earlier.
add_filter( 'the_content', 'my_post_content_remove_shortcodes', 0 );
function my_post_content_remove_shortcodes( $content ) {
/* Create an array of all the shortcode tags. */
$shortcode_tags = array(
'shortcode_1',
'shortcode_2',
'shortcode_3'
);
/* Loop through the shortcodes and remove them. */
foreach ( $shortcode_tags as $shortcode_tag )
remove_shortcode( $shortcode_tag );
/* Return the post content. */
return $content;
}
Wait! Won’t that permanently remove the shortcodes? Yes, it will. Therefore, you must add them back so they can be used outside of the_content. The following code does just that.
add_filter( 'the_content', 'my_post_content_add_shortcodes', 99 );
function my_post_content_add_shortcodes( $content ) {
/* Add the original shortcodes back. */
my_add_shortcodes();
/* Return the post content. */
return $content;
}
Thoughts? Other solutions?
I agree that this seems like a bit of a hack, but it’s the best option I’ve found thus far and doesn’t take much code. I’m open to suggestions on better methods.
The great news is that you don’t have to worry about your users messing up their content by using shortcodes that you didn’t intend for use in the post content. They’ll know upon any attempts to do so that it simply won’t work.
Thoughts?
Just awesome. Never thought of this before.
The actions
loop_startandloop_endare probably safer for this. Don’t think a priority of0is a guarantee: negative numbers are allowed, and they will win.There is one exception: Inside of PHP shortcodes are almost always useless.
echo do_shortcode('[shortcode_1]');is a common and expensive mistake, when people forget they can call the shortcode callback just by its name.Those hooks won’t work if you plan to use shortcodes within The Loop but not within the post content, which is something I often do.
Nevertheless, the priorities only have to be before and after
11, which is whendo_shortcode()is ran overthe_content. I gave a lot of wiggle room for other plugins if they decide to change it.I agree if that’s all you’re doing, you might as well call the function directly.
Hi,
I don’t like the idea to hack this kind of thing. As developer I’d like to use shortcodes as my ‘dev tools’ when creating themes (if necessary). It’s a beautiful approach to execute a function within a string content.
I really prefer to be free when creating shortcodes besides thinking about hacks to avoid bad uses of the shortcode.
I’m curious whether this is purely a semantic argument about theme vs plugin functionality.
Is it the idea that themes shouldn’t influence content only the display of said content? If so, that’s something I can get behind. If not, it seems like such an odd argument to make — no shortcodes in post content — since that’s exactly what shortcodes are for.
I’d also be curious to hear your stance on what shortcodes’ exit strategy should be, especially when people’s post_content is littered with leftover shortcodes. Personally, I’m kind of a fan of re-adding the shortcode and giving it an empty output.
The theme vs. plugin argument is something we take seriously on the theme review team. In the simplest terms, it boils down to presentation vs. functionality. Obviously, there’s some gray area there. If anything, I’m a person who likes to blur those lines from time to time. However, we have extremely strict rules when it comes to themes generating or manipulating content in a way that’s not presentational — in short, don’t do it.
WordPress has a clear functionality layer (plugins) and a clear presentational layer (themes). Until WordPress changes, anyone who is not following those paths is simply doing it wrong.
Themes shouldn’t generate or manipulate content in a non-presentational way. So, yes, themes should “display” content. It’s about data portability. If you switch themes, you lose your data. Of course, you’re also stuck with those ugly, leftover shortcodes that do nothing.
I don’t think there’s a need for an “exit strategy” if you’re not adding them to themes. If I needed one, I’d probably go with your idea since it’s simple to do, but that’s not really a feasible solution for everyone because it’d mean a loss of content.
iam amazed how you cover that and described in a clean way, thank you
Useful! Specially thanks for the shortcodes.
Nice article to cover short codes in word press.. I have been using word press for blogging but never thought of this thing..This looks like more of a techie feedback..would love to follow this blog for more details on Wordpress..
Just awesome, thanks for the tips !
This is good, solid advice and explained very concisely. Thank you sir
It’s good ! Useful! Specially thanks for the shortcodes.