Show all my active WordPress plugins

ript>

Recently, I needed to get a list of every plugin that I’ve activated on all of my WordPress local-hosted blogs.
So, instead of going to every blog’s dashboard and noting down the list of every active plugin, I decided to write a quick (and dirty, i.e. not optimized) script.


<?php
// initialize variables [you have to change them to their proper values]
$dbhost = "localhost";
$dbuser = "dbuser";
$dbpass = "dbpassword";

// connect to MySQL and get all database names
$db = mysql_connect( $dbhost, $dbuser, $dbpass)
      or die( "MySQL connection problem: " . mysql_error());

mysql_set_charset( "utf8");
mysql_select_db( "information_schema", $db) or die( mysql_error());
$sql = "SELECT SCHEMA_NAME FROM SCHEMATA";
$u0 = mysql_query( $sql, $db) or die( mysql_error());
$alldb = array();
while ($u1 = mysql_fetch_row( $u0)) { $alldb[] = $u1[ 0]; }

// select each database found and check for active plugins
foreach( $alldb as $adb) {
    $conn = mysql_select_db( $adb, $db);

    // get *_options table name
    $sql = "SHOW TABLES FROM $adb LIKE '%_options'";
    $u0 = mysql_query( $sql, $db); // or continue;
    while ($u1 = mysql_fetch_row( $u0)) {
        if ($opt = $u1[0]) {
            // get active plugins list
            $sql = "SELECT * FROM $opt WHERE option_name='active_plugins'";
            $u00 = mysql_query( $sql, $db);
            if ($u01 = mysql_fetch_array( $u00)) {
                $apj = $u01[ 'option_value'];
                if ($apj) {
                    $ap = unserialize( $apj);
                    foreach( $ap as $apv)
                        echo "Database: $adb >>> $apvn";
                }
            }
        }
    }
}

mysql_close( $db);
?>

The output of this script is a list of all active plugins, one for each line, so it can be used in a shell script, for example one can find out how many of them are active:

php show_all_active_plugins.php | wc -l

or find out which blogs are using a certain plugin:

php show_all_active_plugins.php | grep akismet

Disclaimer: as usual, this script works for my setup and, for this reason, I'm sharing it here for everyone interested. I hope it will work for anyone else who tries it, but I cannot guarantee it for every possible situation.


Visit The Light of the LAMP blog for more…

5 Replies to “Show all my active WordPress plugins”

  1. Thank you very much for this. I'm going to use a cut down version of it in a WordPress plugin.

    There are two oversights in the script:

    1, You've missed the opening php tag;

    2, To display display the out put in a file called, for example, active-plugs.php, you need to place a line break after each output line.

    BTW, your commenting system doesn't work properly in Chrome.

    Thanks again. I'll be back for future posts.

  2. Thank you Dion, I've made the correction #1.
    As for the #2 remark, I wrote this script to run from the shell, as an argument to php interpreter, so I added only the "n" line break and not a "< br >".

    Feel free to change it and use it as you like, I'm glad it is useful.

  3. This is a horrible example of getting a list of plugins via WordPress. Why wouldn't you use WordPress functions with $wpdb to interact with the database? Obviously you don't write WordPress themes or plugins often.

  4. Kevin you are right and I agree with you, but this is a "quick and dirty" solution, as I wrote at the start of the article, that just works from the command line, where I want it to run (and it runs for all the blogs I have installed).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.