Blogs

Wordpress Developer in San Diego

Use Shortcodes in Widgets Without a Plugin for WordPress

Have ever wanted to use a Shortcode in a widget area of WordPress but didn’t want to add another plugin or contact a developer?

By default, WordPress doesn’t support this feature.

You can simply add this short snippet to your theme’s function.php file and you’ll be using shortcodes in widgets in no time at all!

I always recommend that you do not edit your main themes’ functions.php file, rather, place a new functions.php file in a child-theme folder instead.  This way any updates to WordPress will not break your code.

wp-content/themes/your-theme-here/functions.php  (don’t touch this)

Create a new folder here:

wp-content/themes/your-theme-here-child/

  • notice I added -child  to the end of the new folder

so now you should have

wp-content/themes/your-theme-here/functions.php
wp-content/themes/your-theme-here-child/

Create a NEW functions.php file (use a text editor or notepad) and place it in this folder so it looks like this…

wp-content/themes/your-theme-here-child/functions.php

add this to the functions.php file

<?php 
// Enable shortcodes in widgets
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter('widget_text', 'do_shortcode');
?>

Next…

Open your WordPress Admin –> APPEARANCE —>  WIDGETS  and add whatever Shortcode you want to the Widget. Select a TEXT WIDGET and drag it to the SIDEBAR Widget then add your shortcode

or whatever shortcode you have.

Save it. Then go to your public facing page and you should see the shortcode working!

FIN!

Of course, before making any changes, you should make a full backup of your website. If you don’t want to make a backup from your hosts control panel, you can add a plugin like UpDraft to WordPress to do this for you.

by James Byrne, Information Architect – After Dark Grafx

Categories

RECENT BLOG POSTS