What is a shortcode in WordPress?

A shortcode is a small tag inside square brackets which WordPress replaces with some output when the page is shown. For example shows an image gallery.

To make your own, use add_shortcode(). The function must return the text (not echo it):

add_shortcode( 'hello', function ( $atts ) {
	$a = shortcode_atts( array( 'name' => 'friend' ), $atts );
	return 'Hello, ' . esc_html( $a['name'] ) . '!';
} );
echo do_shortcode( '[hello name="Riya"]' );
Output

Hello, Riya!

See Shortcode API.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.