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.

allow-php-in-widgets

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

[php]

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

[/php]

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

allow-php-in-widgets1

 

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

allow-php-in-widgets-area2

 

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 *