Allow PHP Code in WordPress Widget Area Without Plugin

In WordPress widget area you can use text and html code by using text widget area. You cannot use PHP code in widget area under default WordPress settings. To allow PHP code in widget are you have to either use a plugin or write own function. Go to wordpress plugin directory and search for the plugin PHP allow in widget. But you can allow PHP code in WordPress widget area without plugin. Just adding a simple function you can do the task.


Here is the function. Just copy the code and add this to your theme functions.php file:


add_filter(‘widget_text’, ‘enable_php_code’, 99);
function enable_php_code ($text) 
if (strpos($text, ‘<‘ . ‘?’) !== false) 
eval(‘?’ . ‘>’ . $text);
$text = ob_get_contents();
return $text;


Here is how we test that the code is working fine:



We have added the code in footer area and here is front-end display:



Get more useful query in WordPress Query category.

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *