11.4. WordPress Plugin Deactivation

In the previous blogs, we installed WordPress Multisite and learned to code plugin multisite activation. WordPress Plugin Deactivation follows similar pattern but in Share on Social Plugin it has some minor twists.

Share on Social Plugin uses the same class file class-activator.php for deactivation too. In Sos_Activator::setup() method we register Sos_Activator::deactivate() as the deactivation method using WordPress function register_deactivation_hook().

share-on-social/admin/class-activator.php

class Sos_Activator {

    public function setup () {
        register_deactivation_hook( SOS_PLUGIN_FILE, 
                array( $this,'deactivate' ) );
    }

In the deactivation method, we use WordPress function is_multisite() and parameter $networkwide to call Sos_Activator::deactivate_for_blog() method, which deletes the default basic locker, for site level deactivation either in multisite or in single site installation.

share-on-social/admin/class-activator.php

    public function deactivate ( $networkwide ) {
        if ( function_exists( 'is_multisite' ) && is_multisite() ) {
            if ( ! $networkwide ) {
                if ( false == current_user_can( 'activate_plugins' ) ) {
                    return;
                }
                $this->deactivate_for_blog();
            }
        } else {
            if ( false == current_user_can( 'activate_plugins' ) ) {
                return;
            }
            $this->deactivate_for_blog();
        }
    }

    public function deactivate_for_blog () {
        $this->delete_basic_locker();
    }

For multsite network wide deactivation, we don’t wish to delete the basic locker and hence, Sos_Activator::deactivate_for_blog() is not called.

Purge

During deactivation, customs lockers created by the admins, plugins' settings and stats are not deleted. These plugin entities are purged from database only when plugin is uninstalled.

Purge is deferred till the last stage so that plugin entities are available in case admin chooses to re-activate the plugin.

 
 

 

In the next section, we explain the steps to properly uninstall the plugin.