PHP Widgets

As some of you may know I’m currently in the process of create my very first WordPress plugin (Xbox Achievements).

The idea behind the plugin is you will enter your gamertag in the settings and the rest is done by the app. It will download your game library and achievements for display on your blog.

Achievement Widget
One of the main features I’d like to include is a recent achievement list. Everyone wants to know what the last achievements you earned are? What games you have been playing recently?

So, I decided to include a widget!


I’m not a programmer by trade, I’ve just picked up bits n bobs along the way with the help of some friends.

The plan was to use the text widgets and just implement PHP code straight into them but no! WordPress do not allow PHP code in widgets.

This brought a holt to the whole thing. I didn’t want to hard code it in the sidebar.php as its meant to be a one click install.

So I started to google.

At first I found WordPress widgets that would create a PHP widget. This was perfect for what I needed but I couldn’t be asking people to install two widgets? So then I started thinking about why it worked in that widget.

Basically, it all comes down to this snippet of code:

add_filter('widget_text', 'enable_php_code', 99);
function enable_php_code ($text) {
if (strpos($text, '' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}

Simply add this filter to your functions.php file which is located in your current active themes folder.
It can be found here:

/wp-content/themes/{Your Active Theme}/functions.php

Just add it to the very bottom line of the page and your up and running.

I know this isn’t a fix for the my widgets as I still need people to put this snippet in.

But for anyone out there with access to this file its a nice little add-on to allow php.

Leave a Reply

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