How to use add_shortcode on WordPress

Shortcodes are very useful when you’re working on building a plugin for WordPress.  Especially if your plugin is related to video or audio.

A shortcode on wordpress looks like this:


and you can configure your plugin to easily read this shortcode and value using the ‘add_shortcode’ function inside your plugin.

add_shortcode( 'mycustomshortcode', 'mycustomshortcode_function' );

This works almost like magic!  When adding ‘add_shortcode’ to your plugin, you’re defining both your shortcode and the function that will handle the value and return the results.  So it is just a matter of defining your function and doing whatever you want to do inside it:

function mycustomshortcode_function (  $atts, $content = null  ) {
//My code here

So let’s say you want to convert a video for youtube using shortcodes:

Declare this on your plugin:

function replace_youtube_video( $atts, $content = null ) {
 return '<iframe width="560" height="315" src="'.$content.'" frameborder="0" allowfullscreen></iframe>';
add_shortcode( 'youtube_video', 'replace_youtube_video' );

Then on any page inside WordPress, add this shortcode:


You’ll see how the shortcode transforms itself on a nice youtube vide player.

Go and try it, have fun with it and let your friends know about this! more people should know how WordPress takes care of shortcodes for us