0

I'm using Airtable to keep a real-time list of plugins for multiple WP sites, and have an almost-working setup with Zapier emailing new plugin data when a plugin is activated or deleted.

The email is correct upon plugin activation, however upon deactivation the recently-deactivated plugin is still included in the list. Is the plugin list somehow being cached?

Almost-working code:

////////////////////////////////////////////////////////////    
// send an email on plugin activate / deactivate 
////////////////////////////////////////////////////////////    

function urlToDomain($url) {
   return implode(array_slice(explode('/', preg_replace('/https?:\/\/(www\.)?/', '', $url)), 0, 1));
}

function detect_plugin_change( $plugin, $network_activation ) {

    $url = urlToDomain(site_url());

    $the_plugs = get_option('active_plugins'); 
    sort ( $the_plugs );
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$value);
        $plugins .= $string[0] .", ";
    }

    $to = [email address removed];
    $subject = $url;
    $body = $plugins;
    $headers = array('Content-Type: text/html; charset=UTF-8');

    wp_mail( $to, $subject, $body, $headers );

}
add_action( 'activated_plugin', 'detect_plugin_change', 10, 2 );
add_action( 'deactivated_plugin', 'detect_plugin_change', 10, 2 );

This sends an email like this:

To: [email address removed]

Subject: sitename.com

Body: admin-menu-editor, display-posts-shortcode, gravityforms, list-pages-shortcode, plugin-central, simple-history, taxonomy-list-shortcode, user-switching, wordpress-seo, wp-scss

Any ideas what needs to be edited so that it works properly upon plugin deactivation? Thank you!

  • 1
    What about the bulk activation/deactivation case, e.g. if you bulk deactivate 10 plugins, wouldn't that mean 10 emails? – birgire Feb 7 at 8:42
  • Yes, an email is sent for each plugin, including bulk activations/deactivations. I'm not that worried about it though - zapier will process them all into Airtable. – razorfrog Feb 7 at 15:43
1

If you look here in /wp-admin/includes/plugin.php

        do_action( 'deactivated_plugin', $plugin, $network_deactivating );
    }
}

if ( $do_blog )
    update_option('active_plugins', $current);
if ( $do_network )
    update_site_option( 'active_sitewide_plugins', $network_current );

The options aren't updated until after the hook fires.

You can get around this by using the first parameter to deactivated_plugin:

function detect_plugin_change( $plugin, $network_activation ) {

    $url = urlToDomain(site_url());

    $the_plugs = get_option('active_plugins'); 
    sort ( $the_plugs );
    foreach($the_plugs as $value) {
        // Skip the deactivated plugin.
        if ( $plugin === $value ) {
            continue;
        }

        $string = explode('/',$value);
        $plugins .= $string[0] .", ";
    }

    $to = [email address removed];
    $subject = $url;
    $body = $plugins;
    $headers = array('Content-Type: text/html; charset=UTF-8');

    wp_mail( $to, $subject, $body, $headers );

}

Edit: You can also get "fancy" and provide more details about the plugins, if needed, using get_plugin_data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.