WordPress Managed Hosting - 40% Off For 4 Months - Coupon Code: BFCM2021 Avail Now

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.

Create Faster WordPress Websites!

Free eBook on WordPress Performance right in your inbox.


    Create Faster WordPress Websites!

    Free eBook on WordPress Performance right in your inbox.

      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

        WordPress Help Zone - Ultimate WordPress Pit-Stop

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