WordPress - What Is Shortcode In WordPress? How We Can Use It? - TemplateTrip Help

Father's Day sale

Limited-time deal: Up to 70% off Hurry before it ends!
Save Now

Days

Hrs

Min

Sec

Product successfully added to your shopping cart.

WordPress – What is shortcode in WordPress? How we can use it?

Table of Contents

In a nutshell, a shortcode is a small piece of code, indicated by brackets like [this], that performs a dedicated function on your site. You can place it just about anywhere you’d like, and it will add a specific feature to your page, post, or other content.

WordPress filters all content to make sure that no one uses posts and page content to insert malicious code into the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.

Basically, it allows developers to add their code inside a function and then register that function with WordPress as a shortcode, so users can easily use it without having any coding knowledge.

Adding a Shortcode in WordPress Posts and Pages

First, you need to edit the post and page where you want to add the shortcode. After that, you need to click on the add block button to insert a shortcode block.

WordPress - What is shortcode in WordPress? How we can use it? - TemplateTrip

After adding the shortcode block, you can simply enter your shortcode in the block settings.

WordPress - What is shortcode in WordPress? How we can use it? - TemplateTrip

Add a Shortcode in WordPress Theme Files

Basically, you can add a shortcode to any WordPress theme template by simply adding the following code.

 

Create Your Own Custom Shortcode in WordPress

If you are comfortable with writing PHP code, then here is a sample code that you can use as a template.


// function that runs when shortcode is called
function blog_shortcode() { 
  
// Things that you want to do.
$message = 'Hello world!'; 
  
// Output needs to be return
return $message;
}
// register shortcode
add_shortcode('blog', 'blog_shortcode');

You can now use add this shortcode to your posts, pages, and widgets using the following code:

[blog]