Quick Tip: Allow PHP in WordPress Widgets

Did you ever want to use a little PHP inside a WordPress Widget, only to have it not display on your site? That’s because, by default, WordPress will ignore the code added to the widget.

Add PHP to text widgets in WordPress

Well there’s a really easy fix for that. Just copy and paste the below code into your theme’s functions.php file, upload the changes, and try it out.

The Code

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

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

Now, when you add a text widget to your site, you can use PHP.  For instance, if you wanted a widget that automatically updated the copyright year every January, simply use the below:

&copy; <?php echo date("Y") ?>

I found this tip on Binary Turf and it works like a charm.

2 Responses to “Quick Tip: Allow PHP in WordPress Widgets”

  1. Judd

    Legend. This was a quick easy fix so that I could put a dynamic year into the client’s footer. Thank you!


Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>