Do shortcodes affect performance? each plugin using this technique has to parse post/page content looking for the shortcode? If so, it would be better to limit as much as possible shortcode-based plugins?
3 Answers
You need to look at this from a couple of perspectives and then you have to weigh the odds against each other
In general, shortcodes will be slower (we are talking milliseconds here) as they need to be parsed and processed by the shortcode handler.
USAGE: [my_shortcode]
in post editor
Shortcodes in general are quite useful and usally outweigh the fact that they tend to be tad slower than just hardcoding a function and calling it in the desired template. Shortcodes are portable (that is why they should be in a plugin) which makes them excellent for usage between themes. More so, you do not need to alter any template in order to do something special in a template, you can simply drop the shortcode into the post editor's content meta box and your done.
For convenience and ease of use, shortcodes are definitely the way to go here, and all the positives here outweigh the loss in a mere few milliseconds of time due to not using a spaghetti callback function on its own
USAGE: do_shortcode()
hardcoded in a template
This is where I would draw the line, if you need to call a shortcode via do_shortcode()
, then you are doing it wrong. As I said, shortcodes are a tad slower, so here it would be way better to just write a simple function, drop it into a plugin (which is recommended for portability) or functions.php
, and then call the function into your template.
Either way, you will have to alter the template to either call your shortcode or the function or even class which you created. This is why, IMHO, you should not be using shortcodes, but rather a simple function or a class which is a bit faster.
CONCLUSION
Should you stay away or limit the use of shortcodes? The answer is simple, it all depends on usage as described above. Just a final note, if you are going to call multiple shortcodes within a page, try to limit those by combining features you need into one solid, clever shortcode which can do everything you want.
Before I lay my pen down, I want to leave you with this, download and install the Query Monitor plugin. I personally feel that every developer should have this plugin installed on his/hers local test enviroment. This plugin will tell you excatly how many queries are being made, time take to make those queries, and it will also tell you how much time it took to load a specific page.
-
Thanks a lot this post deserves being published somewhere! Chapeau! BTW I am using P3 Performance profiler to make tests. Any worth it in your opinion?– RiccardoCommented Jun 3, 2016 at 7:56
-
I haven't used that plugin before, but if it does the same job as Query Monitor, then it is good to go. ;-) Commented Jun 3, 2016 at 7:58
-
1Peter thanks a lot again! I've just read your profile, you're a great man! Have fun :-)– RiccardoCommented Jun 3, 2016 at 8:00
There are several aspects and as always, performance is something that have to be taken in a context. Pieter's answer gives you one - "do you even have a (better) way to achieve the required functionality". going beyond that, there are some facts that is worth to keep in mind
- Modern PHP is fast. It is never a good thing to write inefficient code but it is probably not worth wasting too much time trying to optimize the internal working of PHP (example single quote vs double quotes)
- The time consuming part of using regular expressions to detect and parse shortcodes is not always with parsing the text itself but in generating the internal "parsing machine" which is used in the parsing of the text. PHP helps here by caching the machines.
- bloat is bloat, and bloat usually impacts performance in some way. In general you could "turn off" shortcodes that you do not use and get the relevant "parsing machine" to be smaller and therfore faster. The thing to be careful about if you actually go that way, is to make sure that the bloat removal code do not take more time to execute than the impact of the mere existence of the useless shortcodes.
If your not called shortcodes, this means they not called their functions and therefore it no affect performance of your website.
Also you can use static class and variables to reduce load to your site. Every shortcode will be use function only one time, independently how many times you call it.
-
-
Yes, i try it. See, every plugin use add_shortcode function, yes? But if you not use this shortcodes in posts, templates or anything else, this means you shortcodes is only registered and not uses. All right? :) Commented Jun 3, 2016 at 7:18
-
Yes, but I am talking about using plugins that need shortcodes in the post/page content...– RiccardoCommented Jun 3, 2016 at 7:19
-
I think parse shortcodes wordpress core and it is not large time procedure. Plugins must only registered their shortcodes. Commented Jun 3, 2016 at 7:21
-
You mean that WP will parse content, so the content is parsed only one time?– RiccardoCommented Jun 3, 2016 at 7:22