PHP Text Widget — Allow PHP In WordPress Text Widget

February 1, 2011

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

 

Join 37,807 others and get free tutorials & tips on design & development using Wordpress on, Thesis & Genesis!

Need help with Wordpress, Thesis or Genesis?

Are you a designer or a developer looking for a hand with Wordpress, Thesis or Genesis? Or perhaps want to save time with a project? Get in touch and let's get the ball rolling! Check out our Thesis development, PSD to Thesis & other services.

{ 2 comments on PHP Text Widget — Allow PHP In WordPress Text Widget… read them below or add one }

Jamie July 2, 2011 at 10:50 am

Thanks for the tidbit of code. This did the trick for me.

John November 20, 2011 at 4:40 pm

You are awesome… Thanks…

Comment on PHP Text Widget — Allow PHP In WordPress Text Widget

You can add images to your comment by clicking here.

Previous post:

Next post:

X

Wordpress, Thesis, Genesis tutorials from Binary Turf — one tutorial a day. Subscribe & sharpen your web design skills.

Name:

Email:

We respect your email privacy.