WordPress is loved by both professional and amateur webmasters because of the ease with which new functionality can be added with plugins and widgets. But there's a catch: not all plugins and widgets are able to insert content anywhere you want on a site, and they are often constrained to displaying custom content in pages, posts, or sidebars only.
Fortunately, you can use shortcodes in order to use custom pieces of code outside of a page, post, or sidebar – such as in the header or footer of your site. In most cases, plugins are developed to be used within WordPress’ visual editor, and widgets usually add code to the sidebar. With shortcodes, you can embed content directly into any WordPress template file, which will be displayed exactly where you place it. This article will focus on the do_shortcode function and how to use it to add new functionality anywhere you want.
To understand what the WordPress do_shortcode function does, let's look at what a shortcode is. Shortcodes are small lines of text that can be added to your site which represent larger sections of code that are processed upon page load. When used, they invoke various functions that have various results (e.g. embedding a video or an interactive map). You can either use shortcodes written by you or you can use a plugin that automates the process and adds the shortcodes on its own.
The main functionality that WordPress do_shortcode adds to your site is embedding content easier into a theme file, whether it's in the header, footer, or somewhere within the post loop. Basically, you get to insert content on your blog in a dynamic manner. WordPress' do_shortcode function allows users to use shortcodes to personalize and improve their 乐竞电竞 the way they want, by selecting which shortcode to appear where. It is important to mention that some WordPress plugins are used to generate shortcodes for different purposes, but you still need to implement these codes yourself. The only issue with WordPress is that you might not know where to place your shortcodes.
Another caveat is that when using the do_shortcode function, WordPress has to look through all possible shortcodes with RegEx to find the one that is being invoked, making this method somewhat slower than crafting a new, custom PHP function for what you want to achieve. However, as long as you don't go too wild with your shortcodes, the performance hit shouldn't be too bad.
Normally, shortcodes can only be used in the WordPress editor. For instance, if you’d like to place a WordPress do_shortcode in the header section of your theme, you’ll have to do so manually.
Of course, you’ll have to be prepared to tweak some code by copying and pasting a line. The do_shortcode function can invoke any function you need to add to the website, as well as different features on different theme files, without the usual restrictions. The WordPress do_shortcode function looks something like this:
[?php echo do_shortcode("[shortcode]"); ?>.
Don’t use the do_shortcode function if you only want to alter HTML markups. You should use it when you want to:
The 3-step guide to using the do_shortcode function
You can learn how to use the WordPress do_shortcode function very quickly. Follow these simple steps to use any shortcode on your site, using the header location as an example:
Once you’ve found the exact do_shortcode function that you want to call, you need to find the header.php file in WordPress. The easiest way is to use WordPress' built-in theme file editor (Appearance > Editor).
The theme you are using might alter where you find the header.php file or how it looks . Normally, the code in the header.php file contains multiple div sections. These sections are responsible for how your website looks. The do_shortcode function should be added within the div that is linked to the location where you want the function to appear. Here’s an example of using do_shortcode to add the Contact Form 7 on your site:
Once you understand the usefulness of the WordPress do_shortcode function, it will be difficult to stay away from using it.This short tutorial should have answered all of your questions regarding how to use WordPress shortcodes in any section of your site, and how to keep them working despite changes to your theme, plugins, or the core WordPress codebase.
If you enjoyed reading this article on how to use the WordPress do_shortcode, you should check out this one about how to make fewer http requests .
We also wrote about a few related subjects like how to remove proudly powered by WordPress , how to reset WordPress , how to change fonts in WordPress , WordPress favicon , how to duplicate a page in WordPress and how to get a WordPress mega menu .