Wordpress Plugin Installation Hackery
Update: Full script posted below.
I’m just wrapping up a relatively large project centering around a Wordpress plugin. I’ve gotten a chance to explore the API in depth, and have discovered a lot of nice things and a lot of not-so-nice things. One of the not-so-nice things was the way plugin activation is handled…it is assumed that, assuming no fatal errors occur, that a plugin was activated properly every time. There’s no feedback mechanism, no way to pass back a message saying “woa, something’s wrong here”. So I wrote my own.
Now, this plugin will be installed on one site, and I will be doing the installation, so realistically there’s not too much of a concern. But I was slightly worried that the plugin made a lot of tables, and that in the future the plugin may be installed on a system that has a table with the same name. This may be incredibly unlikely, but it’s good to plan. With that goal in mind I wrote a neat piece of code (see below for the whole script to put things in context, though this piece is the most interesting):
It’s a pretty simple concept with some neat implications. Here’s what normally happens when you activate a plugin:
- Wordpress sends you to the current with action=activate to activate the plugin
- On that page, wordpress sets a redirect header to the current page with error=true
- Wordpress includes your plugin file
- If your plugin has a fatal error here, the script dies after sending the redirect header, so it redirects back to the current page with error=true
- Since the plugin hasn’t actually been activated yet, Wordpress doesn’t try loading the plugin anymore, but instead shows an error message and includes an iframe for the current page with action=error_scrape. On this loading of the page the script will include the plugin again, this time actually outputting the errors it gives.
- Assuming the plugin doesn’t cause a fatal error, the system (in this order) activates the plugin, calls the plugin’s activation hook, and overrides the previous redirect to a happy “Plugin activated” page.
Knowing this, it’s pretty easy to use it to our advantage. All we have to do is set an option to record our error and throw an E_USER_ERROR, which is the same thing as an E_ERROR, only we’re allowed to throw it mid-script. Then, when the script is loaded again to display this “fatal” error, we can display our error. Since we threw the error after the plugin was activated, it’s important (in most cases) that we deactivate ourselves.
The end result? A customizable plugin error message. Pretty neat IMHO.
Here’s the full script:
Hi - thanks for posting this. I was trying to solve this exact some problem. You said “see below for the complete script” but it doesn’t seem to be here. I think I’m missing something important using just the snippet you’ve posted, as I’m getting this error:
Fatal error: Call to undefined function deactivate_plugins()
Could you post the complete script, or email it to me? Thanks very much!
Sorry about that Mike…I’ll post the full script tomorrow when I get a chance. Your error, however, seems to either have nothing to do with my script (since my script doesn’t touch deactivation at all), or is because I haven’t tested it in 2.7 yet.
We’ll see as soon as I get a chance to post the script.
Thanks,
Brian
I fixed the fatal error “Call to undefined function deactivate_plugins()” by including the following at the beginning of my plugin script (Wordpress 2.7):
include_once(ereg_replace(”wp-content.plugins.*”, “”, __FILE__) . ‘wp-admin/includes/plugin.php’);
I see…some kind of issue with include ordering. Strange…