A shortcode in WordPress is a simple code that can be used to display dynamic content within posts and pages. Here’s an example of how to create a shortcode in WordPress.
-
Open the functions.php file of your WordPress theme.
-
Add the following code to the functions.php file:
function example_shortcode() {
return "This is an example of a shortcode.";
}
add_shortcode('example', 'example_shortcode');
- Save the functions.php file.
Now you can use the shortcode in any post or page on your WordPress site by entering [example]
where you want the shortcode to be displayed.
In this example, the function example_shortcode
returns the string “This is an example of a shortcode”. The add_shortcode
function maps the shortcode [example]
to the example_shortcode
function.
You can modify the example_shortcode
function to return any content you want, such as HTML, images, or even dynamic data from the database.