How to add shortcode in your wordpress theme or plugin

Shortcode is really important part of codding. Sometimes you need to execute code through shortcode on any event or through any filter. Since Version 2.5 WordPress supports shotcodes.

Before using shortcode you should keep these things in your mind:

  1. Don’t use any general used shortcode name. Because shortcode uses hook so if any other plugin or theme functions uses same shortcode name then your shortcode will be overwrite of those.
  2. If you are using any attributes in shortcode then these should be in lower case.

Add_shortcode is a function which use to add hook for shortcode. It uses 2 parameters.

  1. Tags
  2. Function name
 add_shortcode( $tag , $func ); 

Here is a simple example to use shortcode.


function shortcode_function( $atts ) {

     return "name = {$atts[name]}";

}

add_shortcode('shortcode_fun', 'shortcode_function');

 

So now you can use this code with the shortcode [shortcode_fun].