Create Your First-Ever WordPress Shortcode

Create Your First-Ever WordPress Shortcode - Know Online Marketing

Spread the Love :)
  •  
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
    2
    Shares

WordPress Shortcode

Shortcode is a small piece of code indicated by square brackets [sample]. It performs some specific operation that is assigned to that. This is mainly used to reduce the recurring activity while creating new posts or pages on your WordPress website. Let us show you how to Create Your First-Ever WordPress Shortcode in this article in less than 10 minutes.

The shortcode can be placed anywhere on the website like posts, pages, widgets, footer and more. If you are a newbie or an online marketer to WordPress, it gives an idea of shortcode creation.

If the operation that you are going to do is simple, you can go ahead and create your custom shortcode to reuse whenever and wherever you want. No need to install any plugins here. It’s good news, right?

Create Your First-Ever WordPress Shortcode

In this article, let us show you how to add simple text and an image to the shortcode and adding the shortcode to the post.

Let’s say, at the end of every post you manually add content like this ‘Thanks for reading this article. If you have any queries please reach out to us at admin@knowonlinemarketing.com‘. We can add this to the shortcode and use it at the end of the post. So, no need to add such a lengthy line every time.

Relevant Post: How to Display Random Affiliate Banner Ads Without Plugin?

Step 1 – Navigating to functions.php file

Open the WordPress dashboard and navigate to the functions.php by following the below path.

  • Appearence -> Theme Editor -> Theme Functions (functions.php)

Step 2 – Adding the code to functions.php file

Copy the below code at the start of the functions.php as shown in the below screenshot.

You can edit the text within the $content variable.

<?php
function myshortcode(){
   $content = "Thanks for reading this article. If you have any queries please reach out to us at admin@knowonlinemarketing.com";

return $content;
}
add_shortcode('post-end-text', myshortcode);
?>
WordPress Shortcode Creation - functions.php
WordPress Shortcode Creation – functions.php

post-end-text is the shortcode that we created. You can add this shortcode within the square bracket and can be added to the posts as shown in the screenshot.

Suggested Post: How to find IP with IP Find API?

Step 3 – Adding the shortcode in the post

Once you have added the code in the functions.php file. Then it’s time to use the shortcode in our post as shown below.

  • Add shortcode block under widgets section
  • Add the shortcode post-end-text within the square bracket like [post-end-text]
  • Save the post and preview the changes.
WordPress Shortcode Creation - Adding to the post
WordPress Shortcode Creation – Adding to the post

You May Like: Poptin Popup Maker

Step 4 – Previewing the result

Below is the preview of the changes that we made.

WordPress Shortcode Creation - Result
WordPress Shortcode Creation – Result

Step 5 – Using an image in the shortcode

Instead of using the text you also can try replacing the image by pasting the below code.

<?php
function myshortcode(){
   $content = "<img src='https://knowonlinemarketing.com/wp-content/uploads/2020/07/Know-Online-Marketing-Logo-SVG.svg'>";

return $content;
}
add_shortcode('post-end-text', myshortcode);
?>

That’s it! You’re done with your first-ever shortcode. You can now add the created shortcode anywhere on the site.

You Might Be Interested In: UTM Builder

Conclusion – Create Your First-Ever WordPress Shortcode

Creating simple shortcodes as shown above are highly recommended. Because if you are adding any plugin to create a shortcode that may slow down your website.

There’re a lot of resources and tutorials on the internet. You can just go ahead and learn to develop further complex shortcodes according to your need. Check out the below video. It helps create a shortcode in WordPress.

The same shortcode can be inserted by creating our custom plugin instead of adding the code to the functions.php file. You can also try that out. If you would like to know how to create the shortcode by adding out the new custom plugin, just comment below. So that we can post another article on this.


Spread the Love :)
  •  
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
    2
    Shares

Leave a Reply

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