How to Create WordPress ShortCodes Properly |

Congrats, You are Subscribed to Receive Updates.

How to Create WordPress ShortCodes Properly

Short-codes are more helpful while creating a plugin or theme for your WordPress CMS. Many of plugin developers using this functionality to make their plugin and theme effective.  It is a very simple to create and manage it.  WordPress have some built in API’s for Short codes. You can ge tmore details about pre defined short codes and its usage from here. And Shortcode introduction from WordPress codex.

In default, WordPress Provides few Shortcodes to manage  posts. Here are the list of the features .


 ShortCode Creation : 

Its even simple to create a shortcode, Just a simple line will create a short code,

add_shortcode("kv_gallery", "kv_shortcode_box");

Here, add_shortcode is the function to create shortcode, First parameter, ” kv_gallery” , is the short, we can just just like this to use this kv_gallery effect. “ [kv_gallery] “.    And the second parameter, “ kv_shortcode_box” is the function name which will perform the shortcode action.

Example of function creation:

function kv_shortcode_box( $atts, $content = null ) {
	$type =  ($type)  ? ' '.$type  :'shadow' ;
	$align = ($align) ? ' '.$align : '';	
	$width = ($width) ? ' style="width:'.$width.'"' : '';
	$out = '<div class="box'.$type.$class.$align.'"'.$width.'><div>
			' .do_shortcode($content). '
    return $out;

is_null( $content ) can be used to distinguish between the self-closing and enclosing cases. The shortcode macro is used to enclose content, its handler function will receive a second parameter containing that content. User is have some additional parameters to specify the shortcode functionality. so it is necessary to allow for either case by providing a default value for the second parameter to your handler function.  It will be used by the following way.

[kv_gallery type="info" align="alignleft" width="300" ] Content of the box goes here..... [/kv_gallery]

The above example have some parameters to customize the box.  type is the first extracted attribute, this will handle the type of the box, it may be a information, warning, alert, error, and etc.

Align is the box text align, You can specify its alignment also,  width is the Width of the box, here I just specified as 300, its 300px;

Likewise you can create your own short-code functions.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

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


Current ye@r *