Automatically Deactivate Plugin After a Check

I’m trying to get my WordPress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:

        // End if the theme isn't compatible
        if ( FALSE == $this->themesupport['support'] ) { // This test works fine

            add_action( 'admin_init',       array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
            add_action( 'admin_notices',    array( &$this, 'admin_notices' ) ); // I get notices

            if ( isset( $_GET['activate'] ) )
                unset( $_GET['activate'] );

            return;

        } // if()

The method is pretty straightforward:

Read More
public function deactivate_plugin() {

                deactivate_plugins( plugin_basename( __FILE__ ) );

        } // deactivate_plugin()

Putting an echo in that deactivate_plugin method and it gets called. I’ve also tried including the plugins.php file from core with no change.

Related posts

Leave a Reply

2 comments

  1. The following works:

    <?php
    /**
     * Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
     */
    
    add_action( 'admin_init', function()
    {
        if( isset( $_GET['my_deactivate'] ) )
        {
            deactivate_plugins( plugin_basename( __FILE__ ), true );
            $url = admin_url( 'plugins.php?deactivate=true' );
            header( "Location: $url" );
            die();
        }
    });
    

    After activating, enter any admin URL adding ?my_deactivate, e.g.:

    http://example.com/wp-admin/users.php?my_deactivate
    

    Reference:

  2. I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE