Executing php code inside a WordPress widget, without Using any plugin.
Recently in a project i needed to add php code in theme widget area to perform some action. I googled and found many of plugins by that i can achieve what i am looking for, but after some search on google, i found that i don’t need to install any plugin for such a simple task, number of code spinets can perform this with perfectly.
Same with other developer in WordPress may need to execute custom php code in a widget area in your theme, because you want to display different information according to your site need.
Here i am showing some working and useful code snippets to Executing php code inside a WordPress widget, without Using any plugin.
Just add following snippet to your current theme’s functions.php file:
(Use only one of them from below)
Solution 1
1 2 3 4 5 6 7 8 9 10 |
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; } |
Solution 2
1 2 3 4 5 6 7 8 9 |
function php_text($text) { if (strpos($text, '<' . '?') !== false) { ob_start(); eval('?' . '>' . $text); $text = ob_get_contents(); ob_end_clean(); } return $text; } |
Solution 3
1 2 3 4 5 6 7 8 |
function php_execute($html){ if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html); $html=ob_get_contents(); ob_end_clean(); } return $html; } add_filter('widget_text','php_execute',100); |
Solution 4
1 2 3 4 5 6 7 8 9 |
add_filter('the_content','execute_php',100); add_filter('widget_text','execute_php',100); function execute_php($html){ if(strpos($html,"".$html); $html=ob_get_contents(); ob_end_clean(); } return $html; } |
[paypal-donation]
I am so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs.
Things are very open and intensely clear the problem with clear explanation. was truly information. Your website is very beneficial.