I'm in the middle of revamping a legacy WP-site which has a lot of custom shortcodes and apparently due to how the code is currently organized, these shortcodes are taking a toll in terms of performance.

Sure, I can fix the poorly optimized code and live with the dozen shortcodes and call it a day, but I'm wondering how can I organize them better.

According to WordPress' documentation, it's recommended to place them in a plugin and initialize them on init. Can we reduce the load in this hook by "namespacing" them, like this?

[com.company shortcode attr="attr" prop="prop"]

Has anyone experimented with such solutions? Any drawbacks (except the overhead because of the namespace)?

  • How would namespacing reduce load? – Jacob Peattie May 4 '18 at 6:24
  • @JacobPeattie According to Xdebug Trace logs, initializing this many shortcodes hurting the site's performance. My idea is to initialize only one shortcode (com.company) and move all functionality to its attributes. Does this answer your question? Should I clarify my OP? – fabrik May 4 '18 at 7:02

Unfortunately, this is not how shortcodes work. Your solution will not reduce performance problems.

At initialization the only thing WordPress does is make a list of all shortcodes that are being registered. If you look at the order in which WordPress is loading stuff you will see that plugins are loaded before the theme and init hooks. This means that if you try to do things in a plugin which WP is no expecting at that time, nothing will happen.

One of those things is adding shortcodes. So, in a plugin you must postpone adding the shortcode until the init hook. That is what the example you are referring to means. It has nothing to do with performance. Just registering a shortcode causes no load on the server.

WP only starts doing stuff with a shortcode once it encounters one in a post. That's when inefficiently written code will be taking its toll. The only way to reduce that is rewriting it (or removing it). You can use a plugin like T&P profiler to pin down which functions are eating most server time (disclosure: I wrote that plugin and yes, it still works, even if it hasn't been updated for some time).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.