How to use shortcodes in WordPress sidebar widgets

Shortcodes are a fantastic WordPress feature that allows you to embed pre-defined snippets of code into posts, pages and certain areas of your theme layout.

If you wish to add a shortcode to a widget, you have a number of methods at your disposal.

Embed a Shortcode in a Text Widget

The simplest method for using shortcodes in WordPress sidebar widgets is to create a new Text widget in Appearance > Widgets, then copy and paste the shortcode into the text box.

WordPress widgets

Save the widget, then test it by viewing your site’s front-end.

Embed a Shortcode in a Custom HTML Widget

If you find that the shortcode fails to activate using a Text widget, or you wish to add some extra code, drag and drop a Custom HTML widget into the sidebar instead.

By default, shortcodes are disabled from being activated in a Custom HTML widget, but you can enable this functionality by adding code to your theme’s functions.php file.

You can download the file using an FTP client, then edit the file locally using a text editor or simply use cPanel File Manager in the HostPapa Dashboard to edit the file directly on the server. Log into the dashboard, then select My cPanel. Click Files > File Manager to proceed.

File Manager

Head to your WordPress site root, then select wp-content > themes.  Open the directory pertaining to your active WordPress theme. Select the functions.php file and click Edit in the command bar.

cPanel File Manager

Copy and paste the following code at the bottom of the functions.php file:

add_filter( 'widget_text', 'do_shortcode' );

functions.php

Save the file, then add your widget.

WordPress widget

Once again, test the shortcode works by viewing your site’s front-end.

If you notice any problems or if you need any help, please open a new support ticket from your HostPapa Dashboard. More details on how to open a support ticket can be found here.

Was this article helpful?

[was-this-helpful]

Related Articles

Leave A Comment?