We can easily extend the site functionality without repeating ourselves with the help of a shortcode. Nowadays, most plugins provide shortcode functionality for better reusability. In this article, we will see what is shortcode and how to use shortcode in WordPress? So, let’s start by exploring the basics of shortcodes!
Table of Contents |
1. What is Shortcode |
2. How to Write Shortcode |
3. Built-In Shortcodes |
4. How to Create Shortcode |
5. How to Use Shortcode |
01 What Is Shortcode
The shortcode definition varies in different people. We define it as like shortcode is a WordPress feature that helps you to overcome your repetitive task with very little effort.
Another simple definition: shortcode is a short name of your bunch of a function’s code that allows you to execute anywhere in WordPress by just calling that short name.
02 How To Write Shortcode In WordPress
A shortcode is written inside two square brackets. For example, the "[sc_show_gallery]"
shortcode can be used to add a photo gallery of images to any page, post, or any other place in WordPress.
03 Built-In Shortcodes In WordPress
[audio]
[caption]
[embed]
[gallery]
[playlist]
[video]
We can also pass attributes into the shortcode as the following example shows:
[gallery id="77" columns="4" rows="2"]
04 How To Create Shortcode In WordPress
As the name suggests add_shortcode() function is used to add the shortcode in WordPress. It accepts two parameters. The first parameter is the name of the shortcode and the second parameter is a callback function’s name.
Syntax: add_shortcode( 'name of shortcode', 'callback function name' );
Example of the shortcode:
function dynamic_copyright_year(){
return date('Y');
}
add_shortcode('copyright_year', 'dynamic_copyright_year');
On the above example, we have created a copyright_year shortcode that will print the current year when the shortcode executes. Let’s run the shortcode.
05 How To Use Shortcode In WordPress
A do_shortcode()
function is used to run/execute the created or built-in shortcode. As we have seen above that shortcode written inside two square brackets so here we have to use two square brackets while executing.
Syntax: do_shortcode( '[name of shortcode]' );
Example:
echo do_shortcode('[copyright_year]'); // This will print current year.
That’s it for now. We hope this article helped you to understand the basic concept of shortcode in WordPress.
Additionally, read our guide:
- How To Add Back Button In Elementor
- 403 Error When Updating In Elementor
- How To Add Multiple Post Types In Posts Widget In Elementor
- How to Add Products Per Page Dropdown in WooCommerce
- “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
- How to Create a Plugin in WordPress from Scratch
- How to Disable Admin Bar in WordPress Without Plugin
- How To Send Custom Emails in WordPress
- How to Allow Preview of Draft Post Without Login in WordPress
- Import Users From CSV In WordPress Programmatically
- Dynamically Populate A Select Field’s Choices In ACF
- How To Create Database Table In WordPress
- Difference Of require, include, require_once And include_once
- PHP: Get Day Name From Date
- PHP: Get Year From Date
- How To Update Pivot Table In Laravel
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!