WordPress Hosting Coupon Code | 3 Months | 100% OFF Get Offer

How to Use WordPress do_shortcode

Owais Alam — August 27, 2019 3 Minutes Read
How to use WordPress do_shortcode

For many WordPress developers, shortcodes are a great way of extending the core functionalities of the WordPress core. In fact, all popular plugins now use shortcodes to integrate their features and options within posts and pages of the site.

Now, what if you need to use shortcodes on the website other than the posts and pages. This opens up a host of possibilities for using shortcodes almost anywhere on the website.

In this short tutorial on WordPress do_shortcode(), I will highlight several ways in which you can leverage this great idea into your website.

Custom Message do_shortcode

A very simple use case of WordPress do_shortcode() is adding a custom message “A simple do shortcode demo” at the location of your choice. The following snippet inserts the message or keyword at the location where the shortcode is placed.

In the above code snippet, wp_do_shortcode is the name of the custom function that integrates do_shortcode() functionality into your website. The return statement contains the actual message or keyword that needs to be inserted.

Finally, in the add_shortcode(), you can see that the actual shortcode to be inserted is “do_shortcode”.

As you can see in the following screenshots, when the shortcode, do_shortcode is used:

WordPress do_shortcode

You can see that the shortcode gets replaced by the message in the return statement.

Integration of do_shortcode

Adding Parameters to the do_shortcode Function

Now that you know the basic structure of a do_shortcode() and how to use it in your code, I will present another example that allows you to use the idea more effectively. In this example, I will demonstrate how to add the dimensions of an image to the page. For this, check out the following code:

As you can see, the function parameter_att_do_shortcode() takes the parameters in the $atts. In order to make use of the user-provided parameters, I have used shortcode_atts() that takes in user-provided attributes and fill in the gaps with default arguments.

Build a Custom do_shortcode Plugin

If you wish to create a plugin to add do_shortcode functionality to your website, the process is pretty straightforward.

The process of creating the plugin is simple. Just create a new folder in the wp-cont/Plugins. in the new folder, create a file named plugin-name-seokeyword.php. open the file and add the following code snippet to it.

do_shortcode Integration plugin

Template to Register the Shortcode

Before using the shortcode plugin, you need to register the shortcode. For this, you need to create a separate file with the following function wordpress_do_shortcode_form().

Wrapping Up

Shortcodes are a popular way of adding functionalities to the WordPress core. With the above-mentioned solution, you can extend the usability and application of shortcodes to all areas of the WordPress website. If you need help in implementing the idea at your site, do let me know in the comments.

Subscribe to Get a FREE WordPress Ebook Right in Your Inbox

WPblog provides the complete guide to launch your WordPress website completely FREE!

Owais works as a WordPress Community Manager at Cloudways and contributes occasionally at WPblog. He spends his time developing different websites on WordPress and finds special interest in WooCommerce.

THERE'S MORE TO READ

Newsletter

WP HelpZone - Ultimate WordPress Pit-Stop

Learning WordPress? Or are you expert enough to help others? Join our WP Facebook group!

Also be a part of Giveaways & Discounts exclusively for YOU!