Creating Shortcodes on WordPress

A shortcode is a string that will be replaced by dynamic content. Example: [video src="video-source.mp4"].

In this tutorial, we will create a “Hello World” shortcode.

1. Self-closing tag: [hello]

2. With attributes: [hello color="blue"]

Default attributes with shortcode_atts(...):

Security – Always escape vars when printing:

3. Enclosed Content tag: [hello]World[/hello]

The second argument has the content:

4. Many tags, one callback:

You can define just one callback for many different tags. In this case, use the third argument $tag to differentiate between tags.

Let’s implement two tags, [hello] and [world], with a single callback:

Close Menu