Add a shortcode in WordPress

Shortcode is the code made shorter to do what you specify in the funtion.php file or a plugin. It means that instead of having to write long and messy code, shortcodes help keep it short and easy to write when calling.

Example: With my FAQ Schema plugin, I have made the complex codes of Java script and Microdata shorter and easier to work when you want to structure Shema for FQA.

How does a shortcode look like? A shortcode comes with two square brackets at both sides and a name in the middle, for example: [myshortcode] is one of them.

add shortcode

The way to insert a shortcode into PHP WordPress

If you want to insert the shortcode into a PHP file of WordPress, you must use the do_shortcode () function for it to execute.

<?php echo do_shortcode('[your-short-code]'); ?>

The method to add shortcodes into WordPress posts at Edit mode

Gutenberg blocks of WordPress allows us to edit and insert a lot of codes easily, including shortcodes.

Method 1: Add shortcodes into a post or page using Shortcode block

  1. You navigate to: “Add block” at the cross (+).
  2. Type shortcode to find the Shortcode block.
  3. Click on it to add to the block.
  4. Paste your shortcodes.
add shortcode wordpress post
insert shortcode in wordpress

Method 2: Add shortcodes into WordPress using HMTL block

  1. You navigate to: “Add block” at the cross (+).
  2. Type HTML to search the “Custome HTML block.
  3. Click on it to add HTML to the block.
  4. Paste your shortcodes.
add shortcode in wordpress

Then, publish or update your post. You have just finished to add shortcodes into WordPress post.

How to create a shortcode for your need

Shortcode is really useful when we want to add some contents that repeat, or any annoucements that appear at many pages.

However, when you want to use a shortcode, you need to use a shortcode plugin or create it for your own. So, you will write some codes or just copy the shortcode example below, and paste it into your function.php file of the theme.

if ( !function_exists( 'thomwebsite_shortcode_exam' ) ) {
	function thomwebsite_shortcode_exam()
	{ $string .= '
		
		This place is the content whatever you want.

	';
	return $string;}
	add_shortcode('firstshortcode','thomwebsite_shortcode_exam');
}

With these codes, when you add a shortcode [firstshortcode] into your posts or pages, it will show the content: “This place is the content whatever you want.”

If you find this article useful, please share it to encorage me to write more. Thank you!

About the author

Thom Ho

A person, who has developed Wordpress websites for more than 7 years, has experience in SEO skills and Wordpress hostings. I like to write some things meaningful. And, I'm happy to support WP beginners.

Leave a Comment