The MU forums have moved to

Programmatically deactivating and reactivating a plugin during updates? (4 posts)

  1. jamescollins
    Posted 15 years ago #


    We operate several WPMU installations, as well as 20+ normal WordPress installations.

    Each of these has the same set of WordPress plugins (Google XML sitemaps, cforms, etc).

    We manage all of these using a SVN repository that contains all the core WP/WPMU files, as well as the standard plugins.

    This makes it easy to ensure that all installations have the same files, and makes deployment of updates as simple as running and "svn up" on each installation.

    When I need to update a plugin to a new version, I check the changes into our SVN repository and "svn up" all our installations.

    This works fine in most cases, however some plugins need to be deactivated and reactivated during the plugin update process in order to update properly.

    For example, up until recently our sites all used cforms v8.7. We wanted to update to cforms v9.3, but in order to do this, the cforms plugin must be deactivated and reactivated in order to migrate the settings to the new storage format.

    We have over 100 individual WPMU blogs, and 20+ standalone WordPress installations, so going through each of these and deactivating and reactivating the cforms plugin would be very painful.

    Does anyone know of an easy way to programmatically deactivating and reactivating the plugin?

    I know we could use Plugin Commander to do a batch deactivate and batch activate, but not every blog needs the plugin activated.

    We currently have a common PHP file that is included by every theme we use, and this file stores a COMMON_DB_VERSION constant that can be used to perform update tasks if it's value is different to the value in the wp_options table (similar to WP's db_version option). However I am not sure of the best way to automatically deactivate and then reactivate a plugin.

    I tried manually running the 'activate_cforms/cforms.php hook', but this didn't seem to work either:

    The cforms plugin is just an example of one plugin that needs this deactivation/reactivation procedure. There are many others too.

    Any help would be appreciated.

    James Collins

  2. dsader
    Posted 15 years ago #

    I wonder if the following snippet run through WPMU PowerTools would help?

    $plugins = get_option('active_plugins');
    foreach ($plugins as $plugin) {
    if($plugin == 'cforms/cforms.php')
  3. Ovidiu
    Posted 15 years ago #

    you could have a look at this plugin, and may use it or just some code out of it:

    its a plugin that when activated, deactivates all other plugins and remembers them. when you activate this plugin again, it restores the former state :-)

  4. jamescollins
    Posted 15 years ago #

    Hi dsader/Ovidiu,

    Thanks for your replies.

    I have done some more research, and I've concluded that it is not possible to deactivate and reactivate a plugin within the same page load.

    The WP 2.7 plugin updater deactivates the plugin, and then displays an iframe that is responsible for activating the plugin again (see the wp_update_plugin() function in wp-admin/includes/update.php).

    After doing some more testing, I am now using the following function to deactivate and activate a plugin (if it is already activated):

     * If a plugin is activated, deactivate and reactivate it.
     * Used when performing WP plugin upgrades.
     * @param string the plugin file name (including subdirectory)
    function maybe_deactivate_and_activate_plugin($plugin) {
    	do_action('activate_' . $plugin);

    In the cforms example, the following code should programmatically deactivate and reactivate the plugin (thus invoking the cforms activation/update procedure):


    If the blog/site doesn't have the cforms plugin activated before the update, then no hooks will have been set for the 'activate_cforms/cforms.php' hook, so the function will do nothing.

    If the cforms plugin is activated before the update, the function should run the plugin activation routines.

    I hope someone finds this useful.


About this Topic

  • Started 15 years ago by jamescollins
  • Latest reply from jamescollins