Where does core WordPress generate its shortcodes?

New shortcodes are added to a WordPress site using the core add_shortcode function. This function requires two parameters - the name of the shortcode, and a function for it to call.

This called function will generate the HTML to output when an admin uses the new shortcode in an article. For example, turning a YouTube URL into an embedded video.

WordPress does not have a particularly robust framework, meaning that in third party plug-ins, such functions can end up being called in many different places. However, you can usually do a search for "add_shortcode" within an extension in order to find how and where the developer has created them.

When standards are not specified in the software's documentation, it's usually worth a look at what core code is doing and copying that approach, as this will tend to show the intended way of doing thing.

For example, if we look to how the [video] shortcode is created in WordPress:

  • The add_shortcode function itself lives under wp-includes/shortcodes.php
  • This file also contains several other functions for implementing shortcodes, such as checking the called shortcode actually exists. If you want to know exactly how shortcodes are executed by WordPress, most of the code for doing that can be found here. However, most developers building WordPress extensions will not need this level of knowledge.
  • The [video] shortcode itself is created under wp-includes/media.php using the code add_shortcode( 'video', 'wp_video_shortcode' );
  • This file also contains the associated wp_video_shortcode function to generate different code depending on the parameters sent to it, eg self-hosted video, Vimeo URL, YouTube URL etc.

Whilst media.php is the only place in core WordPress which currently generates shortcodes, the name of this file suggests you should add them alongside other functions grouped together based on area of use. In this case, [video] is batched with other media widgets [audio], [caption], [gallery] and [playlist].

Looking at the Jetpack plugin, which is made by Automattic (owners of the hosted version of WordPress, and so who are likely going to follow internal WordPress best-practise), we can see that most shortcodes are created under modules\shortcodes\TYPE.php - with the TYPE being things like Spotify, Twitter etc.

I've seen suggestions that you should create a single file to house all of your plug-in's shortcodes regardless of their purpose (e.g. shortcodes.php), but Jetpack's approach of having a shortcodes folder with individual files named after a general use seems the more tidy and professional one.

Return to Blog