WordPress Shortcode.

Since Version 2.5 WordPress supports so called ‘Shortcodes’.
Example:

[audio]

Shortcodes can also be used with attributes as the following example shows:
Example:

[audio id="123"]

WordPress have some of the shortcodes by default (for example the audio one).
The following shortcodes are included with WordPress:

  • [audio]
  • [caption]
  • [embed]
  • [gallery]
  • [playlist]
  • [video]

hortcodes are of both type singular and paired.

We can define custom shortcodes using add_shortcode
Example:

Calling Shortcode

In Php:
do_shortcode('[MyFirstShortcode]');
In Editor:
[MyFirstShortcode];

Defining Shortcode


add_shortcode('MyFirstShortcode','MyFirstShortcodeCallback');
function MyFirstShortcodeCallback() {
    echo 'hello';
}

Shortcodes with attributes as the following example shows:
Example:

Calling Shortcode

In Php:
do_shortcode('[MyFirstShortcode title="This Is Shortcode"]');
In Editor:
[MyFirstShortcode  title="This Is Shortcode" ];

Defining Shortcode

add_shortcode('MyFirstShortcode','MyFirstShortcodeCallback');
function MyFirstShortcodeCallback($attr) {
    echo $atts['title'];
}
//output This Is Shortcode

Leave a Comment

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

Scroll to Top