What is a shortcode?
A shortcode is a small word inside square brackets, such as [gallery]. When you type it in a post, WordPress swaps it for something bigger, like a photo gallery or a contact form. It is a shortcut, like a nickname that stands for a long name.
What is do_shortcode?
Shortcodes work by themselves inside posts and pages. They do not work inside theme files. A theme file is a file that builds your site design, such as header.php. do_shortcode is a function, which is a ready-made command in PHP. It tells WordPress to run a shortcode from inside those files. PHP is the language WordPress is written in.
Steps
- Find the shortcode you want, for example
[gallery]. Plugins list their shortcodes in their help pages. - Open your child theme's file where it should appear. This may be
header.php,footer.phpor a page template. You can use the File Manager in your hosting control panel. - Find the spot in the file where the output should show.
- Add the code below.
- Save the file.
- Open your site and check the result.
<?php echo do_shortcode('[gallery]'); ?>
This line runs the gallery shortcode and prints the result in that spot. The word echo means "show this on the page".
Shortcodes with settings
Some shortcodes take settings, called attributes. Put them inside the brackets. Use single quotes outside and double quotes inside.
<?php echo do_shortcode('[gallery columns="3"]'); ?>
This shows the gallery in three columns.
Use it in a widget
A widget is a small box in a sidebar or footer. Many sites already run shortcodes in a Shortcode block. You can add that block without code. Click the plus button in the editor and search for "Shortcode".
If nothing shows
- Check that the plugin for that shortcode is active.
- Check the spelling of the shortcode.
- Check that your quotes are straight, not curly.
Quick recap
- A shortcode is a small bracket word that stands for bigger content.
do_shortcoderuns a shortcode inside theme files.- Use
echo do_shortcode('[name]');. - Back up and use a child theme before you edit files.