Create Custom WordPress Shortcode Plugin

WordPress introduced shortcode given that variation 2.5. Produce personalized WordPress Shortcode is a particular code that lets you do to set features for producing code with really little initiative. Shortcodes can embed data or create objects that would normally need great deals of made complex.

WordPress Shortcode put right into post, web page and also various other content, it means to instruct WordPress to call macro code by using a trivial shortcode square brackets [mygallery] and Shortcodes can also be used with additional attributes [mygallery id="123" size="medium"].

In this tutorial, we will speak a bit a lot more regarding how to create personalized WordPress Shortcode plugin. Then, we’ll reveal you just how straightforward it can be to program your own in the adhering to actions. Let’s begin!.


Step 1: Create Shortcode Function 

First, go to directory wp-contentthemesyour active theme name then open up with your favorite text editor a function.php file of the active theme. Create or write new php function with a unique name which will execute the code you’d like the shortcode to trigger.

<?php
  function otnix_shortcode() {
     return 'Hello Otnix...!!!';
  }
?>

This function can also take custom parameters, if you want custom to add a parameter into the function, you might add an attribute parameter. These parameters are called attributes, and they are all handled using a single array predefined as $atts by WordPress.

<?php
function otnix_shortcode( $atts = NULL ) {
   $a = shortcode_atts( array(
      'name' => 'world'
   ), $atts );
   return 'Hello ' . $a['name'] . '!';
}
?>

This defaults the name attribute to world if attibute null value. If a name is provided, the function will output that instead. Shortcode square brackets as the following.

[helloworld]

Shortcode square brackets with attribute parameter.

[helloworld name="Otnix Team"]

Step 2: Register Shortcode Function

The last step is to register your new shortcode with the Shortcode API. This is done with the add_shortcode function. It requires two arguments:

  1. The shortcode tag to be used within the text editor
  2. The name of the function handling the execution of the shortcode

Beneath your shortcode function, include this line of code and update it to match your own values:

add_shortcode('helloworld', 'otnix_shortcode');

The first worth will go within square braces in the full-screen editor. The secondly will certainly match the feature name you wrote in the previous 2 actions. Both must be one-of-a-kind sufficient so they don’t interfere with other shortcodes included by WordPress core, styles, or plugins.


Step 3: Use WordPress Shortcode

When you have actually completed, here the instance to put a shortcode square brackets right into the WordPress editor.

Create Custom WordPress Shortcode
Use WordPress Shortcode

If this article Create custom WordPress shortcode plugin could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *