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:

  1. WordPress sends you to the current with action=activate to activate the plugin
  2. On that page, wordpress sets a redirect header to the current page with error=true
  3. 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.
  4. 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:

Tagged with:  
Share →

7 Responses to WordPress Plugin Installation Hackery

  1. Mike Toppa says:

    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!

  2. Brian says:

    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

  3. madmaxmatze says:

    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’);

  4. Brian says:

    I see…some kind of issue with include ordering. Strange…

  5. [...] activation in a separate process, making it difficult to communicate anything back to the user. Brian Krausz solved this problem, and my solution for safely checking PHP compatibility builds on his [...]

  6. [...] But Words: How to Make a PHP 5 WordPress Plugin Die Gracefully in PHP 4 Nerdlife: WordPress Plugin Installation Hackery Share and [...]

  7. Mike Toppa says:

    2 years later… I thought I’d let you know I found a simpler way to do this, based on your idea. I also noticed someone asking about this in the support forum. See my reply in the forum here for an example:

    http://wordpress.org/support/topic/feature-request-error-messages-on-plugin-activation-deactivation?replies=5

    If you’ve learned any other ways to do this, please let me know. Thanks!

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>