How To Create Shortcode in WordPress

WordPress Shortcode API

WordPress Shortcode API is complete set of methods that are used to create shortcodes in pages , posts and custom posts. For example, the following shortcode will add gallery with the attached images. Shortcode API enables developer to create specific content that a user cam insert into desired pages or posts.

[gallery id="123" size="medium"]

The API Shortcode handles  both kind of shortcodes self-closing and enclosing shortcodes by crafty parsing, thus eliminating the need for writing a regular expression for each shortcode. Functions are included for setting and retrieving predefined attributes.

Here’s a minimal example of the PHP code required to create a shortcode in wordpress:

function demo_func( $atts ){
	return "foo and bar";
}
add_shortcode( 'demo', 'demo_func' );

This will create [demo] shortcode that returns as: foo and bar

Comments are closed.