How to Use Shortcodes in WordPress Page, Theme, Widget & PHP

If you are running a WordPress website or blog, then you must know what the shortcodes are. Now let’s see how to use shortcodes in WordPress Pages, template, theme, widgets and PHP.

Using shortcodes in WordPress is pretty easy when it comes to adding them in pages or posts. But sometimes it can be a little tricky especially when you try to use them outside of the WordPress editor. So here I will show you how to use shortcodes in WordPress pages, templates, themes and widgets. Let’s dive into this.

Use of Shortcodes in Pages or Posts

Adding shortcodes in WordPress page or post is super easy.

  1. Open the page or post in which you want to add shortcodes.
    add-shortocodes-in-wordpress-page-post
  2. Add shortcodes in it.
    add-wordpress-shortocodes-in-page-post
  3. Just Update it or Publish It.
    add-shortocodes-in-wordpress-page-post-3

It’s that simple.

Use of Shortcodes in a Template or Theme File or PHP

Adding a shortcode in a template file or theme file is not like adding in page or post. To add any shortcode in a template file or theme file, you have to use a function do_shortcode().

This function allows you to use shortcode within a template or theme file or along with php. It will look something like this:

<?php echo do_shortcode( ' [shortcode-name] ' ); ?>

Note: Replace the shortcode-name with your shortcode and use it.

Just copy the above code and paste it into your template file or with PHP code, where you want to add shortcode. Again don’t forgot to change shortcode name

Example:

If I want to add the shortcode in my theme’s footer file then I will:

  1. Open footer.php file via WordPress editor or FTP Applications like filezilla.
    add-shortocodes-in-wordpress-theme-php
  2. Add shortcode in it and just save it.

And I am done with adding the shortcode in template or theme file. Now let’s understand a little bit about the function do_shortcode(). What this function is and what is actually do.

What do_shortcode() do?

do_shortcode() is an inbuilt WordPress function that is used to search for content to the shortcode. This function filters the shortcode output according to their hooks.

Use of Shortcodes in Widgets

Using a shortcode in sidebar widget is as simple as adding in pages or posts.

  1. Select a Text Widget and Add it into the sidebar or any other Widget area, where you want to have your shortcode should be executed.
  2. Then Simply add the shortcode in it and save it.

After the release of WordPress 4.9, adding shortcodes in widgets became so easy. They have made the long-awaited improvements in the WordPress core about the use of shortcodes in widgets.

So now, you can simply use shortcode with custom HTML or without HTML in your WordPress Text widgets. There is no need to add an extra filter in your WordPress themes or plugins to use shortcode in widgets.

Use of Shortcodes in Widgets Before WordPress 4.9

If you are using an older version of WordPress then I recommend you to update your WordPress.

Note: If you are Updating your WordPress, please make sure you have kept the most recent backup of your website including all the files and database.

But if for some reason you don’t want to update your WordPress then you can use the shortcode in text widget as I did above.

  1. Select a text widget
  2. add the shortcode in it and then save it.

But if you want to add custom HTML with your shortcode then you should use Custom HTML widget and by default, shortcodes are not allowed to be executed in Custom HTML Widgets.

So to do that, you have to add this code in your theme’s function.php or your site’s specific plugin file.

add_filter( 'widget_text', 'do_shortcode' );

Once you have done this, simply select a Custom HTML Widget and add your shortcode in it…and Save it.

That’s it! You’re done.

I hope this article helps you to understand how to use WordPress shortcodes in pages, templates, themes, and widgets.

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top