How to write shortcode in WordPress ?
Last Updated :
23 Feb, 2023
If you see the text in brackets, then that is the Shortcode. In this article, we learn about how to write the shortcode in WordPress. First, we discuss WordPress Shortcodes. In WordPress, there are security filters that could not allow writing any dynamic code into WordPress elements like posts, pages, and widgets. That’s why for writing dynamic content WordPress introduces Shortcodes.
What are Shortcodes in WordPress: When you see some text or link is wrapped in square brackets, these are Shortcodes.
Shortcodes are an easy way to add dynamic content to your WordPress posts, pages, and sidebars.
Basically, it allows developers to add their code inside a function and then register that function with WordPress as a shortcode, so users can easily use it without having any
coding knowledge.
Syntax:
[name_of_shortcode]
Why to use Shortcodes: Many WordPress plugins and themes use shortcodes to add specialized content like contact forms, image galleries, sliders, and more.
Word Press filters all content to make sure that no one uses posts and page content to insert malicious code into the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.
But what if you wanted to run some custom code inside your posts to display related posts, banner ads, contact forms, galleries, etc?
This is where Shortcode API comes in; When Word Press finds the shortcode it will automatically run the code associated with it.
Steps to write Shortcodes: Follow the below steps to write shortcodes:
Step 1: Create new theme:
It is good to you that make a custom changes in your child theme. Therefore you cannot lose any file. After installing child theme, to write a Shortcodes you want to open folder name as
wp-content >> themes >> child theme

File structure of wordpress
Step 2: Create Custom shortcode file:
We suggest you, to make a new custom shortcode file to write dynamic content instead of functions.php . That is reliable and make accessible. To make custom file follow the steps below:
- Click on child theme
- Select option create new file
- Give name that file as custom-shortcode.php
To make custom shortcode file accessible you want to include in the functions.php . Write the following code in your functions.php:
<?php
// code
include('custom-shortcode.php')
?>
Step 3: Write shortcode function
After setting up all file you may able to write a Shortcodes. First create a shortcode function in custom-shortcode.php
Syntax:
custom-shortcode.php
Example:
<?php
// code
function my_shortcode(){
$message = "<h3>Hello WordPress</h3>";
return $message;
}
?>
Step 4: Register that function
After creating shortcode function you want to register it using add-shortcode() method. In same custom-shortcode.php file, below shortcode function we register the shortcode function. It takes two parameters one is shortcode name and second is shortcode function name.
Syntax: The below is the syntax of register shortcode function is:
add-shortcode('shortcode_name', 'shortcode_function_name');
Use following code in your program:
add_shortcode('greeting' , 'my_shortcode');
Example: The full code looks like as follow:
PHP
<? php
function my_shortcode() {
$message = "<h3>Hello WordPress</h3>" ;
return $message ;
}
add_shortcode( 'greeting' , 'my_shortcode' );
?>
|
In the above code, the shortcode name is greeting and shortcode function name is my_shortcode
Use shortcode in code: Next step to write a Shortcodes in your program where you want to display the dynamic code. Write a Shortcode_name in square brackets.
Example: In this example the shortcode name is greeting so, we write shortcode as [greeting]

Use shortcode in wordpress elements
Output:

Output of shortcode
Note: You cannot write [shortcode-name] this syntax in .php files directly. It is another way to display shortcode in php.
Syntax: The below is the syntax of shortcode for php files:
echo do_shortcode("[shortcode-name]");
HTML
<!DOCTYPE html>
< html >
< head >
< title >Page Title</ title >
</ head >
< body >
< h2 >Welcome To GFG</ h2 >
<? php
echo do_shortcode("[greeting]");
?>
</ body >
</ html >
|
Output:

Shortcode in php
Similar Reads
How to Embed Video in WordPress ?
WordPress is an open-source Content Management System framework. it is a tool that organizes the whole process of creating, storing, and showcasing web content in an optimal way. WordPress started its journey as an improvement tool to enhance the regular typography of day-to-day writing. But it was
2 min read
How to Add Menu in WordPress?
WordPress is an open-source Content Management System that is based on PHP and MySql which is used to create a dynamic website. Meta Tag generates codes or tags for search engines to pick up your page titles and meta descriptions. These are HTML codes used to describe the content of a page. In this
2 min read
How to set-up Menus in WordPress ?
Setting up menus in WordPressis essential for making your website easy to navigate. For the WordPress website, menus are one of the most important sections for the user's experience and navigating the whole website. WordPress provides the menu editing section in the dashboard so that the users can e
5 min read
How to Install WordPress on Your Website ?
WordPress is a content management system that allows you to host and build websites. WordPress contains plugin architecture and a template system, so you can customize any website to fit your business, blog, portfolio, or online store. WordPress is well-known for its ease of installation. Installing
7 min read
How to Upload Video on WordPress?
Uploading videos to your WordPress website can significantly enhance user engagement and make your content more dynamic. Whether you want to showcase a product demo, a tutorial, or just a fun video, WordPress makes it easy. In this guide, we'll walk you through the process of uploading videos to you
3 min read
How To Use Ajax in WordPress?
Ajax (Asynchronous JavaScript and XML) is a powerful technique used in web development to create more responsive web applications. By using Ajax, you can send and retrieve data from a server asynchronously without interfering with the display and behaviour of the existing page. In WordPress, combini
3 min read
How to Create WordPress Plugin from Scratch ?
Creating a WordPress plugin from scratch might seem tough, but it's an essential skill for customizing and extending the functionality of your WordPress site. This article will walk you through the steps of creating a simple plugin. What is a WordPress Plugin?A WordPress plugin is a piece of softwar
5 min read
How to Add Meta Tags in WordPress?
The process of adding meta tags in WordPress can be accomplished through various methods. By using SEO plugin like Yoast SEO or any other all-in-one SEO pack makes it easy to add meta tags without editing any code. In this article, we explains about adding meta tags in WordPress with different appro
3 min read
How to add a PHP page to WordPress?
Adding a custom PHP page to your WordPress site can be useful for various reasons, such as adding unique functionality or creating custom templates. This article will walk you through the process step by step. 1. Create a WordPress Template PageStep 1: Login to Microsoft Web Matrix: Open or create y
2 min read
How to embed iFrame in WordPress ?
An iFrame (Inline Frame) is a way to embed one web page inside another using HTML code. This allows you to display content like videos, maps, or other websites directly on your page without needing to store these files on your own server. iFrames are handy for showing external content while keeping
3 min read