Support » Plugins » Force Plugin Activation to Fail

  • I am writing a plugin that is reliant on other plugins being activated. Is there a way for my plugin’s activation function to tell WordPress that my plugin should fail to activate? I would then want to print a message to the user about why the plugin did not activate.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I can’t find a particularly good user friendly way to do this, but I can offer what I’ve found so far.

    This (psuedo)code is what I use at the moment for this sort of thing:

    function myplugin_activation_check(){
    	// check to see if this plugin will even work
    	if( check to see if plugin is not going to work ) {
    		deactivate_plugins(basename(__FILE__)); // Deactivate ourself
    		wp_die("Sorry, but you can't run this plugin, for whatever reason.");
    	}
    }
    register_activation_hook(__FILE__, 'myplugin_activation_check');

    Basically it runs that function on activation, and if the plugin decides that it can’t work, it deactivates itself and spits out the big error message. Not the prettiest error, but I’m still working on it.

    Any progress on this?

    There’s a few things that I would like to see. First, WP should have something where you can throw a custom error. In my case, I’m building a plugin that checks for database collisions an activation.

    It’s ugly, but that’s not my biggest issue. My biggest issue is that after throwing such an error, I hit ‘Back’ and the plugin page shows the thing was activated anyway. It’s like deactivate_plugins completely ignores me.

    Does anyone know the reason for this?

    You could try this:

    //requirements check
    if(*there are missing plugin requirements*) {
            echo "Missing plugin requirements\n";
            exit;
    }

    You put this before the rest of the code of your plugin.

    So the result is, that when somebody activates the plugin and there are missing requirements, the plugin activation fails and the error is what you have written in your echo. 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Force Plugin Activation to Fail’ is closed to new replies.