Fun with Widgets

Note: The ExecPHP widget does not work in the upcoming WordPress 2.5. Therefore, if you upgrade to 2.5, you will need to install the updated version of this widget. It can be found here, now and forever more:

A new plugin for WordPress came out that’s kinda cool. It’s called Widgets, and the general idea of it is to make the sidebars on your blog a bit more configurable, a bit more easily.

So, I decided to spend an hour or so converting my sidebars to Widgets so I could manage them a bit simpler. Editing PHP code is easy but time consuming, while dragging stuff and dropping it to rearrange the blog, well, that’s quick and easy.

Few things I learned:

  1. The Widgets Plugin does not like Windows. Since I run this site on a Windows box (edit: I did then, I don’t anymore, and anyway, they have fixed this problem since then), it needed some tweaking to make it work. For those who want to know, the problem is in the sidebar_admin_setup() function in widgets.php. More info on this problem is in the comments of this post.
  2. Text boxes are extremely useful when converting from your existing customized webpage to a widgets based one, as you can simply cut and paste chunks of HTML into them and voila, you’ve got a sidebar widget. However, there’s not a lot of text widgets available. If you need more text widgets, you will need to edit these functions: widget_text_setup(), widget_text_page(), and widget_text_register(). As it turned out, I didn’t need but 4 text widgets because:
  3. Despite how useful text boxes were for a quick migration, they were not enough.

So, I ended up writing two plugins of my own.

The first one is a Google AdSense plugin. You can download it here: It’s exactly the same as the Text widgets, but with (very) minor tweaks specifically for inserting Google Ads in the sidebars. Also, it shows a name of “Google AdSense” in the widgets panel, which is nice. Like the Text widget, you can have more than 1 of them, but unlike the Text widget, you can only have 4 of them, since Google’s TOS only allows 3 ads and 1 link block on a page.

The next one is also like the Text plugin, however it will also let you put PHP code into the text and have it actually work. Here’s the download: Like the Text plugin, you can have up to 9 of them (more if you want to tweak the code). Note that any PHP code you put into the widget MUST be surrounded by <?php and ?> tags, just like writing PHP normally. Also note that this is isn’t exactly safe, as it’s just doing an eval() on whatever you put in there, so you have complete and full access, as does anybody else who can get to your admin screen. Still, it’s very useful.

Both of those are plugins. Just drop them in the widgets folder under the plugins folder, and activate them on the plugins screen. Then the widgets will be available for you to use on the widgets screen.

Hope these help somebody. They sure helped me. )

207 thoughts on “Fun with Widgets”

  1. Thanks a lot !! I’m a complete newcomer to wordpress and spent days and night trying to figure how to insert my html code from amazon or like I did on Blogger ! Thanx a lot ! You saved my next 2 or 3 coming nights for sleeping ! :-))

  2. Pingback: Ashish Jha's Blog
  3. Hi Otto, I write at

    I used the phpcode to pull most commented posts. however, I noticed that if you add another php code widget, for another php function, it displays the exactly the same thing, i.e. the results from the first php code 1 widget. looked at the database, and it stores both php code in the wp_options row. i don’t know now where to look at.


  4. Heri: You are using an older or incorrect version of Widgets. The version of Widgets that was included in the first release of Widgets in the core code had this problem. It was patched for subsequent releases of WordPress.

    In other words, update WordPress to the latest version (2.2.2 as of this writing) and the problem will go away.

  5. If that update fixes your problem, then you need to update WordPress. One version of WP had a bug that required that fix, but the latest version works perfectly with the original version.

  6. Hi Otto,

    I’m currently using 2.3 Beta 3 of WP and am getting the following error when I try to insert my FAlbum php script to display 4 random photos:

    Fatal error: Call to a member function on a non-object in /home/.valenzuela/dcelis/ : eval()’d code on line 1

    Is this compatability issues with Beta 3, or is something wrong just happening?

  7. David,

    No, ExecPHP is perfectly compatible with Beta 3, something’s just wrong with the code you put into the widget. Because your code executes in the context of the execphp plugin, any error in your own PHP code causes the error to appear to come from the plugin. But the key here is that the error is in the “eval()’d code”. Your own code is the code that’s being “eval()’d”.

    Sorry, I know that that doesn’t help much, but it’ll tell you where to look at least. 🙂

  8. Hi Otto,
    it doesn´t works with my WordPressMU 1.2.4. Only text will be saved, no php-code.
    WP 2.2.2 –> it works.

    Viele Grüße,

  9. Hmmm. It seems that when I add a second widget, the results is a repeat of the first one. So I get two instances of the first execphp widget output on the sidebar.

    (I’m using WP 2.2.)

Leave a Reply

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