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:
- 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.
- 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:
- 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.
I wish I had this plugin sooner for one of my clients! Thanks a lot for doing all this work Otto! Great plugin, works as advertised.
I’m having a problem with IE7/Opera and the Exec-PHP (4.2) plugin on latest wordpress 2.3.1. In firefox, it works gloriously. In IE7 and Opera, however, if anything is BELOW the widget containing the php code, the alignment goes to hell and everything is screwy. Im using the cutline 3.1 theme and the widget in question is Star Ratings for Reviews 0.4.
The workaround is to put the Exec-PHP widget as the last widget on the sidebar, but obviously in this case that’s not where I want it to be. =)
I’ve seen you state that it’s the PHP code, but I imagine I dont get why it would work in firefox then. Of course it’s a superior browser, but.. heh.
Thanks for all your hard work! Let me know if you have any ideas.
Without knowing the specific PHP code you’re putting in the widget, I cannot tell you what the problem is. However, it’s absolutely certain that it’s the code you’re putting into the widget. The ExecPHP widget is like the Text widget: by itself, it does absolutely nothing. It’s just a container for your own code.
I suggest running your site through the W3C validator. If your code is valid, it will likely show up correctly in all browsers. If your code is invalid, then it won’t. That’s sort of the point of validation.
Thanks, i just used this plugin.
Yes, having the same problem as Chewru Guru with 2.3.1 wordpress…. any ideas??? I’ve used this plugin for many other sites with older versions of wp just fine.
Brooke: The plugin works perfectly well with 2.3.1, there’s nothing wrong with it. You need to understand that by itself, the PHP widget does nothing of importance. Any code that is created on your page is created by the PHP you put into the widget. If it’s broken, then either your code is broken or the theme is broken.
Otto: Bruce was having a problem with “permissions” with widgets.php. I had a similar problem stating “Method not implemented – POST to /wp-admin/widgets.php”. I suspect others are having similar problems.
The problem was that the “ModSecurity” module in Apache was detecting what it though was a “PHP Injection Attack” when posting (submitting) the php code in your widget. (Normally you would not want a visitor to your website to post php code that would be executed.)
My (crappy) solution was to turn off (comment out) that part (2 lines) of the modsecurity, submit the php widget, then re-enable the modsecurity. It means you have to have access to your Apache config while making changes to the php widget code, OR you need to permanently disable the modsecurity — NOT a good idea!
But if you don’t update the PHP widget code very often, temporarily disabling the modsecurity while submitting the code works okay.
A possible work-around: Do as described above to submit a php code widget that is simply a PHP include command. Then modify the separate php file using FTP, vi, or whatever appropriate tools you have.
Otherwise, after a little digging through error logs and other messing around, the widget works as advertised. THANKS!
Hello Otto,
Excellent plugin. Thanks a lot for that, since i needed it to get the just-as-excellent wp-ajax-newsletter plugin to work (http://code.google.com/p/wp-ajax-newsletter/). One question though: you refer to some safety problem, although you don’t explain this. What’s the danger of using the plugin? For your information: it works at my site, so there’s no problem to be solved. Just curious.
I too had the problem with the code messing up in IE. It was not a php code problem. In the plugin file I had to add a right after the div class execphpwidget. Now everything works beautifully in IE.
I knew it wasn’t the code because all of the various codes I tried from half a different plugins messed up the sidebar. 6 plugin writers couldn’t write php code snippets that poorly in my opinion. 🙂
Glad it’s fixed. Hope this works for someone else!
AllyAnderson: No, I’m sorry, but there is no problem whatsoever with the PHP Code widget. The exephpwidget DIV is coded correctly, and everything inside that DIV is generated by the PHP code that you type into the widget!
@myself
Ok, nothing to do with execphp. Why this confusion ? Because the problem occured when i activated execphp … If you have a dispaly problem with IE (or any other browser but Firefox seems less touchy) when adding a widget with a plugin, you should test adding a built-in wordpress widget at the same place … Correlation is not causality, old story … ;o) Thanks for your time.
Cheers Otto! Fantastic! I had problems getting WordTube to work as a widget (wanted multiple songs listed, not just one), and couldn’t get the multi-version to go into a sidebox. Your widget did the trick 🙂 Now I just have to figure out why anything under the widget is way out of alignment (like off the bottom).
Cheers!
ok, I’m just a goober at this, so here is a link to the code http://www.thecosmeticcounter.com/code.txt
If the function is coming up as undefined like that, then either the other plugin is not activated, or it’s misspelled…
Thanks, I had no clue a plugin like execphp existed but have wished one did for quite some time now.
OMG thanks soooo much!!
I’ve been trying to find a plugin like this for days now 🙂
thanks soooo much for this great plugin
Awesome script, thank you for the resource…perfect size!
http://codex.wordpress.org/Plugins/Plugin_Compatibility/2.5 says this plugin is not compatible with 2.5, do you plan to update it?
That is a great plug in. It is working perfect. I am using it to get the latest posts from my phpBB forum. However, I had to re-install the forum and the widget went crazy. I cannot access my php code in the widget as the code corrupted my widget section in the admin panel. Is there a .php file where my code I entered in the widget is stored, so that I can edit it. As I have no more access to the widget/code from WP-Admin. I can deactivate the plug-in an all is working again but I would like to use it. I tried to override the execphp with a fresh file but the plugin still remembers my code. Where can I edit it ?
Hi Otto,
I was looking for a plugin that does exactly what yours does. My problem is, i have activated it already, but how and where do i put the adsense code? sorry i’m really new at this, hope you could help so I could put up the adsense on the right side of my site. Thanks for your help Otto.
If I want to use php code widget with a theme that is not widget-aware, what should I do? Thanks
Awesome widget! Exactly what I needed. Thanks so much.