How to do I get a list of active plugins on my wordpress blog programmatically?

I have 2 blogs, one which is multisite and one which isn’t. I want to get a list of plugins active on both the blogs so I can compare them.
On the multisite blog, I want to list the plugins which are enabled network-wide as well as site wide.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The activated plugins are stored in the options table of a WordPress Blog under the key active_plugins

so you can use get_option('active_plugins'); of each blog and compare the arrays.

Method 2

In form of a Dashboard Widget, one for Single Sites and Network Sites Dashboard, other for the Multisite Network Dashboard.

/*
 * Single Site Dashboard Widget
 */
add_action('wp_dashboard_setup', 'wpse_54742_wp_dashboard_setup');

function wpse_54742_wp_dashboard_setup() {
    wp_add_dashboard_widget( 'wpse_54742_active_site_plugins', __( 'Active Plugins' ), 'wpse_54742_active_site_plugins' );
}

function wpse_54742_active_site_plugins() {
    $the_plugs = get_option('active_plugins'); 
    echo '<ul>';
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$value); // Folder name will be displayed
        echo '<li>'.$string[0] .'</li>';
    }
    echo '</ul>';
}


/*
 * Multisite Dashboard Widget
 */
add_action('wp_network_dashboard_setup', 'wpse_54742_network_dashboard_setup');

function wpse_54742_network_dashboard_setup() {
    wp_add_dashboard_widget( 'wpse_54742_active_network_plugins', __( 'Network Active Plugins' ), 'wpse_54742_active_network_plugins' );
}

function wpse_54742_active_network_plugins() {
    /*
     * Network Activated Plugins
     */
    $the_plugs = get_site_option('active_sitewide_plugins'); 
    echo '<h3>NETWORK ACTIVATED</h3><ul>';
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$key); // Folder name will be displayed
        echo '<li>'.$string[0] .'</li>';
    }
    echo '</ul>';


    /*
     * Iterate Through All Sites
     */
    global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("
        SELECT blog_id
        FROM {$wpdb->blogs}
        WHERE site_id = '{$wpdb->siteid}'
        AND spam = '0'
        AND deleted = '0'
        AND archived = '0'
    "));

    echo '<h3>ALL SITES</h3>';

    foreach ($blogs as $blog) {
        $the_plugs = get_blog_option($blog->blog_id, 'active_plugins'); 
        echo '<hr /><h4><strong>SITE</strong>: '. get_blog_option($blog->blog_id, 'blogname') .'</h4>';
        echo '<ul>';
        foreach($the_plugs as $key => $value) {
            $string = explode('/',$value); // Folder name will be displayed
            echo '<li>'.$string[0] .'</li>';
        }
        echo '</ul>';
    }
}

Method 3

List of plugins, with sites on which they are used (Multisite only)

If you want to know which plugin is currently activated and on which site, you can use a function like that one:

function wpstars_list_active_plugins() {

  if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {

    echo "<table class='active-plugins'>";
    echo "<tr><th>Plugin name</th><th>Sites</th></tr>";

    $plugins = get_plugins();

    // Network activated
    $active_plugins = get_site_option('active_sitewide_plugins');
    foreach($active_plugins as $active_path => $active_plugin) {

      $plugins[$active_path]['Sites'] = "A,";
    }

    // Per site activated
    $sites = get_sites();
    foreach ( $sites as $site ) {

      $active_plugins = get_blog_option($site->blog_id, 'active_plugins');
      foreach($active_plugins as $active_plugin) {

        $plugins[$active_plugin]['Sites'] .= $site->blog_id . ",";
      }
    }

    foreach($plugins as $plugin) {

      echo "<tr><td>{$plugin['Name']}</td><td>{$plugin['Sites']}</td></tr>";
    }

    echo "</table>";
  }
}

Method 4

WP-CLI is just the ticket. I’ve used if for so many things, I’ve lost count!

wp plugin list --status=active

If you want, you can run these commands on your local machine with aliases

You would then use the @site function

wp @all plugin list --status=active

or

wp @multisite list --status=active
wp @blog list --status=active


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x