$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Send email with list of active plugins upon activationdeactivation|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Send email with list of active plugins upon activationdeactivation

matteradmin11PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far