Are php template shortcodes ok?

Is it Ok?

Yes!

Is it Optimal?

No

What Would Be Better?

Shortcodes map on to PHP functions, so why not cut out the middle man and go straight to the original function?

Are there any other downsides?

Yes!

That shortcode had to come from somewhere, now you have a dependency, maybe the plugin that has the shortcode gets deactivated, who knows

Are There Any Upsides?

Yes! It’s much easier to build a gallery shortcode programmatically then pass it into do_shortcode than it is to create one from scratch.

Even if you do bypass the shortcode and go straight to the original function, you still have the same problem mentioned above. If the shortcode doesn’t exist, it’ll degrade gracefully back to an empty string. You can use the shortcode inside post content too

So, best to use shotcodes ONLY in widgets + pages + posts?

Is it Ok? Yes! But is it the best? Probably not

Shortcodes are awesome! But remember their purpose, if you need to put stuff in a template, you don’t need to use a shortcode, but if needs must, that option’s always there. The do_shortcode function is just a tool, and they’re for embedding things in content, don’t go using it as the foundations for your site or framework. I’ve built plenty of sites, and I’ve not had to do this

Leave a Comment