using PHP code into WordPress widget

Sometime we need to insert PHP code into WordPress text widget but by default, WordPress ignore PHP code into widget. People use plugin for getting this but we can achieve it by using below given code into theme’s function.php.

using-php-code-into-wordpress-widget
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 whenever you use text widget into your site, you can use PHP code. If you want current date to display just use into your widget like this

PHP code into Wordpress widget
<?php echo date("Y") ?>

Using PHP code into WordPress widget is very simple.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.