PHP Text Widget — Allow PHP In WordPress Text Widget

Have you ever wanted to use PHP in WordPress text widgets? All of us have. Here’s a neat little trick to allow you to insert PHP in WP text widgets. Sure, there are several plugins which allow you to do that. But isn’t it neat to use the default WordPress Text widget and give it the PHP dimension? Copy and paste this into your functions.php file and see the magic happen.


add_filter('widget_text', 'php_text', 99);

function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}

Now all the text widgets can use PHP code as long as it goes in the <?php and ?> tags .

Source: PHP Text Widget