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. 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.

  2. Martin: You put the php file into the wp-content/plugins folder. Then you go to the plugins screen and activate the plugin. Finally, you go to the widgets screen, and you’ll have a new widget available for dragging and dropping into the sidebar. When you configure it, it’ll have a textbox for pasting the adsense code into.

  3. Pingback: Wordpress Plugins
  4. Otto, Thank you so much for coming over to wordpress/support to help me with my question. Your answer worked perfectly. I’ll put it here too in case anyone is having trouble with Top Commentators or Popularity Contest formatting.

    Here was Otto’s response:
    You need to edit that widget and add and around the function call that you’re making. Why? Because the html it’s creating has each link inside of an li tag, but you don’t have a list around those for the li tags to make sense.

    Just edit the widget and change this:

    to this:

    Thanks again!

  5. Oops here the advice from Otto

    You need to edit that widget and add and around the function call that you’re making. Why? Because the html it’s creating has each link inside of an li tag, but you don’t have a list around those for the li tags to make sense.

    Just edit the widget and change this:
    // //

    to this:

    //

    //

    forgot to put in the backslashes. The backslashes are like quotation marks around the code. They are not part of the answer.

  6. I’m having trouble using this plugin with WP 2.2. Whenever I have more than one of these widgets in use at one time, the contents of the first to be evaluated are copied to every other instance of the widget.

  7. There’s a fix for WordPress 2.2 that will correct this problem:
    Download this file: widgets.php.
    Replace your /wp-includes/widgets.php with this file. Problem solved for the moment.

    I’ll have a fix to the widget itself that should allow it to work with anything sometime tonight.

  8. Executable PHP widget seems to screw the sidebar up in IE 7. No problem in Firefox. Using WordPress 2.2. Makes the bottom half of the sidebar move to the opposite side and not lined up. Using default theme.

    Here is the code I put in the widget
    ‘ . __(‘Pages’) . ” ); ?>

  9. Steven: The PHP widget is basically identical to the Text widget. It cannot “screw up” anything by itself.

    It’s certainly possible for the code you use to do something like that though. So I’d suggest changing your code.

  10. Getting this error:

    Parse error: syntax error, unexpected T_STRING in /home/.dino/chutney/makingchutney.com/wp-content/plugins/execphp.php(24) : eval()'d code on line 1

  11. sry – this is the whole code, removed the php tags at start and end to get it in the post…

    wp_list_pages(‘exclude=44,45,46,48,50,51,52,53&title_li=’ . __(‘Pages’) . ” );

  12. @chutney: Something is wrong with the PHP code you put into the widget. That’s what causes that error.

    @rogilvie: I can’t tell you what’s wrong with your code, exactly. The widget itself doesn’t modify anything or produce any real output of its own. If it’s breaking the page, it’s your code that is doing it, somehow.

  13. Thx a lot for the solution with the execphp. I use it for my latest Pixelpost photograph to be displayed in WP.

    Best wishes!!
    Jens

  14. I have the problem with WordPress 2.2 and understand that I need to update my widgets.php as per link at the top of this page. Will this effect my current widgets settings?

  15. WordPress 2.2.1 works with the ExecPHP widget as is, no changes required.

    And no, you will not lose any settings by updating the widget.

  16. Tried it on my test site, which has 2.2.1 installed, and got a message along the lines of “you don’t have permission to edit (or access?) widget.php file on this server …”

    Smells like I need to fool with my file permissions but not sure what setting to assign. Currently have set to write by me only.

  17. Bruce: What are you talking about? It’s a plugin. Drop it anywhere in the plugins folder, activate the plugin. You don’t need to edit any widgets.php file or anything like that.

    All the files in the WordPress directory must have at least 444 permissions, most likely, but that’s normal for WordPress.

  18. Otto, not attempting to edit the widget file. That’s what’s odd. I just add the PHP widget to my sidebar, put some code in it, and get the following message

    You don’t have permission to access /keenstuff/kltestblog/wp-admin/widgets.php on this server.

    The code I put in is simple

    Recent Posts

    ">

    Strange. The widgets file is showing a permission of 644.

    I know I am doing something wrong somewhere but just don’t know what. Any suggestions? By the way, I get an odd “you cannot write to such and such directory” for some other admin items,too. Not a pattern. There must be a permission screwed up somewhere along the way.

    Thanks,
    Bruce

Leave a Reply

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

css.php