What is WordPress Shortcodes
WordPress shortcodes are a text patterns(with opening and closing square bracket tags) in post content which are replaced by php functions before displaying to the user.
The above shortcode is converted to something like this by a PHP function
WordPress Built-In Shortcodes
WordPress by default provides a lot of shortcodes which you can use. Here is a list of wordpress built-in shortcodes.
Creating your own Shortcodes
We can also register our own shortcodes. To register a similar kind of shortcode as mentioned above use this code
Put this code in theme or plugin file.
//first attribute is the passed array of attributes of the shortcode
//second attribute is the content inside the shortcode tags
function download_callback($atts=null, $content=null)
//convert array into instance of variables
//echo HTML code
return "<a href='." $url ".'>." $content ".</a>";
//register a shortcode
//first argument is shortcode name
//second argument is php function for the shortcode
Displaying Shortcodes text In Posts
Sometimes we might want to display the shortcode text itself in the post. In that case we can wrap the shortcode inside square brackets.
Here the shortcode itself will be dislayed instead of executing callback and displaying function output.
Shortcode by default cannot be used in widgets(text widget), comments(on front end or backend comments editor) and excerpts. We can enable usage of shortcode in those areas also by adding this code:
//stops wordpress from wrapping shortcode tags separated into multiple lines inside paragraph tags.
//more info check this http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'comment_text', 'shortcode_unautop' );
add_filter( 'the_excerpt', 'shortcode_unautop' );
//enable shortcode in widgets
add_filter( 'widget_text', 'do_shortcode' );
//enable shortcode in comments
add_filter( 'comment_text', 'do_shortcode' );
//enable shortcodes in excerpt
add_filter( 'the_excerpt', 'do_shortcode');