Execute PHP in WordPress text widget

[box]CMS: WordPress  Difficulty Level: Easy  Time: 1 min.[/box]

By default, WordPress text widget only allows to execute the HTML code. If somehow you need to run PHP code in the text widget, you would have to add below snippet of code to your theme’s functions.php file – There are no other settings required. After you have put below code in functions.php file of your theme, the PHP code in the text widget would run automatically.


// Run php in text widget
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;
}

[hr]



Hello my name is Imran Jafri, I am the Editor and founder of Maboot Mag. I am extremely passionate about Web design, UX and GUI. I love to play with PHP, javascript and Wordpress. Of course I am a MAC and using Ubuntu as my secondary OS. That's all.

Share This Post

Related Articles

© 2017 Maboot.com. All rights reserved. Site Admin · Entries RSS · Comments RSS
Powered by WordPress · Designed by Theme Junkie