Shortcode Tutorial – Create Self-Closed Shortcode

Self-Closing shortcodes of wordpress are the basic form of shortcodes. This type of shortcodes doesn’t have any end tag but they can also accept parameter values and they close automatically. It contains the shortcode’s name only.

Syntax

[shortcode_name]

Examples

[audio], [video], ...

Crate a custom self-closed shortcode

We will create self-closed shortcode by creating a custom plugin.

Adding comments to plugin and add function to it.

Create a folder simple-basic-shortcode inside /wp-content/plugins folder.

Next,

Create a file simple-bs.php inside it. Open and write this code into it.

<?php

/*
Plugin Name:  Simple Basic Shortcode
Description: It shows social links of facebook and twitter into website
Author: Online Web Tutor
Author URI: https://onlinewebtutorblog.com/
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: basic-shortcode
Version: 1.0
*/

Add Function into Plugin

function owt_basic_shortcode_social_links()
{

    $facebook = "<a href='https://www.facebook.com/owthub'>Follow Us On facebook</a>";
    $twitter = "<a href='https://www.twitter.com/owthub'>Follow Us On Twitter</a>";

    $output = $facebook . '</br>' . $twitter;

    return $output;
}

Register Shortcode

To register shortcode in wordpress, we use a wordpress function add_shortcode().

add_shortcode('social_links', 'owt_basic_shortcode_social_links');

social_links is the name of shortcode. Keep in mind this must be unique. owt_basic_shortcode_social_links is the name of function.

When we call social_links shortcode, attached function owt_basic_shortcode_social_links will be called.

Here, is the complete code of plugin.

<?php

/*
Plugin Name:  Simple Basic Shortcode
Description: It shows social links of facebook and twitter into website
Author: Online Web Tutor
Author URI: https://onlinewebtutorblog.com/
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: basic-shortcode
Version: 1.0
*/

function owt_basic_shortcode_social_links()
{

    $facebook = "<a href='https://www.facebook.com/owthub'>Follow Us On facebook</a>";
    $twitter = "<a href='https://www.twitter.com/owthub'>Follow Us On Twitter</a>";

    $output = $facebook . '</br>' . $twitter;

    return $output;
}

add_shortcode('social_links', 'owt_basic_shortcode_social_links');

You will see inside Plugins >> Installed Plugins

Activate this plugin to use.

In your post/page you can use this shortcode.

[social_links]

Successfully, we have now created self-closed shortcode in wordpress.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more