How To Use PHP In WordPress Widgets

Recently, I posted an article about using shortcodes in WordPress widgets with a small snippet. PHP is an awesome language to easily create some cool stuff, just like WordPress. In this tutorial, I’ll show you how to use PHP in WordPress widgets.

You can do a lot of great stuff with some small PHP snippets, so why not use them in your WordPress website’s text widget?

Just add following snippet to your current theme’s functions.php file:

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

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

That’s it!

Leave a Reply

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