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:
- Use shortcodes in widgets without a plugin
- 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.
Carrie, i always find awesome stuff on your site. Just a little thank you! 😀
Thanks for letting me hear – I appreciate it! 🙂
Thanks! Took me forever to find this piece of info.
Short and sweet! TKS.
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?
Is it possible to include a php function within a shortcode? I can’t seem to get that to work.
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.
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.
Hey there,
I don’t. I’d find a plugin that does it well and deconstruct for your own purposes. 🙂
carrie
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?
Awesome, thanks. This and your add a CTA widget tut have added a whole new dimension to my designs. Thanks
Would you please tell me how to add link to title wordpres without plugin?