• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Carrie Dils

Carrie Dils

WordPress Developer, Consultant, and Instructor

  • About
    • Work with Me
    • Around the Web
    • Media Kit
  • Blog
  • Podcast
  • Web Development Courses
  • Freelancing Courses

Widgets Gone Wild

Originally published on October 9, 2012 by Carrie Dils
Last updated on July 31, 2017

Today’s tip is quick (and easy!). We’re going to learn how to extend your WordPress widgets without plugins. In 5 minutes or less you’ll know how to:

  1. Use shortcodes in widgets without a plugin
  2. Execute PHP code in widgets without a plugin.

Don’t believe me? Just try it! All you’ll need is your theme’s functions.php file and a party hat.

Use Shortcodes in Widgets Without a Plugin

Ever needed to use a shortcode in a widget area, but couldn’t get it to work? Here’s why: by default, WordPress doesn’t support it. But don’t cry! You can add this short snippet to your theme’s function.php file and you’ll be abusing shortcodes in widgets in no time!

// Enable shortcodes in widgets
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter('widget_text', 'do_shortcode');

Hat tip to Tim Base for alerting me to some issues with shortcodes in widgets on WordPress 4.8+.

Use PHP in Widgets Without a Plugin

Sometimes you might want to execute some php in a widget. You can download plugins to do the job or you can do it yourself with just a handful of code. Add this to your theme’s function.php file:

// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

Done? Now you can pop PHP into a sidebar widget and it’ll work like a charm! If you’d like to know why the code works, I invite you to read this explanation by Emanuele Feronato.

Warning: Use PHP in widgets very judiciously as this is a potential security vulnerability you open by enabling code execution from a widget, versus a file on the server.

Interested in other WordPress Tutorials?

Here are articles specifically for the Genesis Framework and here are general WordPress helper tutorials.

 

Filed Under: WordPress Tutorials

Avatar for Carrie Dils

About Carrie Dils

I believe you can make a good living doing work that makes you excited to get out of bed every morning.

In addition to this blog, I host a podcast for freelancers, teach business courses at The Fearless Freelancer®, and teach WordPress and Front-end Development courses for Lynda.com and LinkedIN Learning. I'm also writing a book for freelancers.

Reader Interactions

Comments

  1. Avatar for Carrie DilsWilliam Smith (@WilliamSmith) says

    April 29, 2013 at 12:18 pm

    Carrie, i always find awesome stuff on your site. Just a little thank you! 😀

    • Avatar for Carrie DilsCarrie Dils says

      April 29, 2013 at 12:44 pm

      Thanks for letting me hear – I appreciate it! 🙂

  2. Avatar for Carrie Dilstnguyen77Tan says

    August 11, 2013 at 1:57 pm

    Thanks! Took me forever to find this piece of info.

  3. Avatar for Carrie Dilsfrancisco rivera (@frajtal) says

    October 24, 2013 at 11:33 am

    Short and sweet! TKS.

  4. Avatar for Carrie DilsAndrew Randazzo says

    November 15, 2013 at 11:17 pm

    This is great! The only problem I’m running into is the shortcode snippet messes up the rest of my widgets by causing them to align horizontally rather than vertically. It also changes the size of my widget titles. Any suggestions how I can fix this?

  5. Avatar for Carrie DilsDarryl says

    January 2, 2014 at 2:58 pm

    Is it possible to include a php function within a shortcode? I can’t seem to get that to work.

    • Avatar for Carrie DilsCarrie Dils says

      January 3, 2014 at 10:00 am

      Yep. That’s actually a much better practice than adding PHP support to widgets (I’ve learned a bit since I originally published this post).

      Check the shortcode API in the codex for examples.

  6. Avatar for Carrie DilsHemant Aggarwal says

    January 20, 2014 at 6:39 am

    Thanks for this great tutorial. Do you have a tutorial on displaying list of posts in one post with shortcodes. I found many plugins for that, but I want to do it manually instead.

    • Avatar for Carrie DilsCarrie Dils says

      January 21, 2014 at 12:58 pm

      Hey there,
      I don’t. I’d find a plugin that does it well and deconstruct for your own purposes. 🙂

      carrie

  7. Avatar for Carrie DilsRon says

    January 30, 2014 at 1:57 am

    Hi,This is great.
    I have a doubt..I install a plugin on my site , and it contain a short code like [wpgc id=”1″].So for getting result i need to past this short code on corresponding post.It is working perfectly..Now i need to convert this short code to php code .so i create a new page in that plugin folder namely camp.php & it contain

    but when i run that page then “Call to undefined function do_shortcode()” error msg.
    any idea?

  8. Avatar for Carrie DilsDom Wint says

    February 20, 2014 at 8:19 am

    Awesome, thanks. This and your add a CTA widget tut have added a whole new dimension to my designs. Thanks

  9. Avatar for Carrie Dilshttp://quocmarketing.com says

    February 20, 2014 at 9:43 am

    Would you please tell me how to add link to title wordpres without plugin?

Primary Sidebar

Search

Heya, I’m Carrie Dils

Carrie Dils chillin at the beach

Articles

  • Business
  • Front-End Development
  • Genesis Framework
  • Mental Health
  • Web Accessibility
  • WordPress Tutorials

Things I Love ❤️

My Tech Toolbox

Favorite Business Books

  • OfficeHours.FM
  • My Courses on LinkedIN Learning
  • The Fearless Freelancer®

© 2021 CWD Holdings LLC