[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]
