Pass PHP Code Inside of a WordPress Widget [without plugins]
I don’t consider myself a “theme developer”. I’m not spending months developing a bulletproof theme, reselling it and then providing continuous support for hundreds of users on Theme Forrest. Rather, I build custom themes that are tailored (fig 1.0) to my clients specifically. That being said, I don’t always give them access to “everything”…but one area I find they nearly always ask to take control of is the footer.
bloginfo();
. Well, if you’ve ever tried to pass PHP in a WordPress widget, you know the results aren’t pretty.
Solution
Here is a simple way to turn the WordPress default text widget into a PHP enabled widget…and it’s NOT a plugin!
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; }
Explanation
I originally found this code over at Emanuele Feronato’s blog, where he has written up a very detailed explanation of what this function is doing. So, for the sake of time I’m going to redirect you there if you want more information: Executing php inside a WordPress widget.
I have tried it personally, this code is fully functional in widget section. But i want to use it in post and pages. Can you tell me, how to do it or email me the way to do it.
Hi Bunny,
I would suggest trying one of these plugins to accomplish that: http://wpsnippy.com/plugins/9-plugins-to-excecute-php-code-in-wordpress-post-page-and-widgets/
It won’t work in post and pages. For post and pages, you need to use a plugin called “PHP Code for posts
“. It works fine and i have personally used.
Badass! Thanks so much. Going to Ajaxify my Woocommerce shopping cart and NEEDED THIS! Didn’t know I could do this.
Glad to have ben able to help!
This is great and its working on my project. Thank you
You’re welcome, Marshall…glad to help!