Wednesday, November 19, 2014

How to Run PHP Code in Text Widget Without Using Plugin

WordPress does not allow to run the PHP code within the text widget. However, one can easily solve this issue by adding a small number of codes into functions.php file.

Copy and paste the below code in your functions.php file to enable PHP code within WordPress text widget.

//Execute PHP code in WordPress text widget
function execute_php_text_widget($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('widget_text','execute_php_text_widget',100);

Paste  "Hello World!" program inside a text widget to test whether the code is working or not.

 //Hello World
<?php echo "Hello World!" ?>


Check out the below image which is a perfect output for this. 


0 comments:

Post a Comment