php - Send email with list of active plugins upon activationdeactivation

admin2025-06-03  2

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

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!

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

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!

Share Improve this question asked Feb 7, 2019 at 5:58 razorfrograzorfrog 154 bronze badges 2
  • 1 What about the bulk activation/deactivation case, e.g. if you bulk deactivate 10 plugins, wouldn't that mean 10 emails? – birgire Commented Feb 7, 2019 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 Commented Feb 7, 2019 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748936051a314965.html

最新回复(0)