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
1 2 3 4 5 6 7 8 9 10 11 | 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
1 | <?php echo date("Y") ?> |
Using PHP code into WordPress widget is very simple.