WordPress.org

Ready to get started?Download WordPress

Forums

Force Plugin Activation to Fail (4 posts)

  1. theirishthug
    Member
    Posted 5 years ago #

    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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  3. Miah
    Member
    Posted 5 years ago #

    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?

  4. Iva Koleva
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic