Create shortcode in wordpress

Shortcodes are WordPress – specific code that enables you to do nifty things with little effort, such as embed content or create objects that would normally require lots of complicated, ugly code. In this article, you learn how to allow users of your plugins to enhance their posts with advanced customizable content using just a few characters.A shortcode is a simple tag syntax between square brackets, such as  [something] , used in posts. At render time when the post is displayed, the shortcode is dynamically replaced with a more complex and user – defined content.

In this article i am going to create shortcode in wordpress for related posts. This plugin work on your single page. When you place this shortcode on your single post page then it will get all the relative posts.

 

How to create shortcode?

There are some built-in shorcodes in wordpress so you can not create your custom shortcodes with these name. for example,

You can see the full list of built-in shortcodes here.

 

Create custom shortcode

The above code simply print the message “This is testing message”. There are two ways to add your shortcode in content.

  • In the back-end editor
  • In the page with this function do_shortcode().

 

In this tutorial I have create a simple plugin that help you to fetch related posts on single post page.This is not very complicated plugin as seen in the code. Here is the full code of the plugin.Make sure you have activated the plugin.

 

In the above code,

This code helps you determine your plugin by wordpress. This above code is known as plugin header.

 

Main functionality:

add_shortcode() function help you to create a new shortcode in wordpress. You can read more about this function here.

 

Show on Frontend

At the single post page, Add the following shortcode on your single page so that this shortcode get the result.

 

Some useful functions
  1. add_shortcode()
  2. is_single()
  3. get_the_category()
  4. query_posts
  5. General loop
  6. get_the_ID()
  7. get_the_post_thumbnail()
  8. get_the_permalink()
  9. get_the_title()