Skip to content
Related Articles
Open in App
Not now

Related Articles

How to write shortcode in WordPress ?

Improve Article
Save Article
  • Last Updated : 22 Dec, 2022
Improve Article
Save Article

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:

  1. Click on child theme
  2. Select option create new file 
  3. 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 Shortcdes. 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:

  • custom-shortcode.php

PHP




<? php
    // code
      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 Shortcdes 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:

ShoShortcode  iin php


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!