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: http://wordpress.org/extend/plugins/php-code-widget/

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: gadsense.zip. 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: execphp.zip. 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. hi, i just wanted to download exephp.zip. the link seems to be broken??

    can you send me the zipfile to me email adress? would be sooo great!!!!!!!

    thanks kris

  2. I’m having trouble with what should be quite simple — the plug-in is downloaded and it resides in the proper directory, but it won’t show in the list of available plug-ins. Help?-rob.

  3. Never mind the above; for whatever reason, the problem vanished after re-downloading, replacing the file, trashing the cache, and reloading the page.-rob.

  4. When I paste my adsense script in the adsense widget and click save changes, on re-opening the text disappears!

    Should I do something to encode/escape the HTML tags?

  5. Pingback: the SmockLady
  6. execphp is a great little plugin! You can see the Categories block I created with it on my left sidebar at potluck.com — it shows only the top level items, no subcategories, just what I’d wanted.

    I wonder, though — on the “Potluck Creative Arts” page and all its subpages, I’d love for an image to show up *instead* of the Category block. I tried this:

    “;
    } else {
    echo “Categories” . wp_list_cats(‘sort_column=ID&children=0&list=1&hide_empty=0’) . “”;
    } ?>

    …and I got close. The image shows up correctly on the appropriate pages instead of the category block. However, on all other pages/posts, the category block goes crazy — instead of showing up the way it does right now on the site, the “Categories” heading appears at the very bottom, below all the category items, and each category item is formatted as if it were its own little sidebar block, with the graphic outline. Looks terrible! Any idea what I could do, or is that just too much a general php/CSS question for me to ask here w/r/t to the execphp plugin?

  7. I realized that the code I’d put in my last comment didn’t come through in tact. In any case, I suspect it’s beside the point. The real question may be this: Would it be possible to alter the execphp widget so that it included conditionality? E.g., in addition to the title and code boxes that currently exist, there could be another box at top which allowed the option of attaching conditional code. If left blank, the widget would function as it does now. If filled it, the conditions would be assessed, and only if true woud the box then be added, again just as it works now, but if the conditions proved false, then no box would be added to the sidebar at all. This would probably solve everything I need, and it would open up a world of possibility for this widget’s usage!

  8. Pingback: expose
  9. Just what I was looking for. I just updated my theme to a widget based, and had to move all my custom menu. Thanks, and keep up the good work.

  10. I’m new to WordPress, how do I exactly put my AdSense code into my sidebar using gadsense.php?. I know basic PHP and HTML, but I do not know WordPress Plug in API. I’ve also activated widgets plug in successfully.

  11. Every time I try to put the php code on it using your widget it says: The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
    No matter when I try it. It says that EVERY time.
    if you could please email me if you know what’s wrong, i would really appreciate it. (the-musicinmyheadAThotmail.com)
    thanks

Leave a Reply

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

css.php