web analytics

Updates from July, 2011 Toggle Comment Threads | Keyboard Shortcuts

  • Apostolos Tsompanopoulos 17:26 on 2011-07-27 Permalink

    Show all my active WordPress plugins 

    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.

    // 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());
    $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…

    • Dion de Ville 16:02 on 2011-12-16 Permalink | Reply

      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.

    • Apostolos Tsompanopoulos 16:26 on 2011-12-16 Permalink | Reply

      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.

    • Kevin L. 16:07 on 2012-02-03 Permalink | Reply

      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.

    • Apostolos Tsompanopoulos 16:12 on 2012-02-03 Permalink | Reply

      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).

    • Tim Kaufmann 12:46 on 2012-05-17 Permalink | Reply

      This comes in handy when you don't want to run the wordpress installation after it has been infected by a malicious hacker.

  • Apostolos Tsompanopoulos 06:21 on 2007-09-18 Permalink  

    Enable warnings in MySQL 

    By default my MySQL setup didn’t display warnings. So, when this morning I tried to update a record with a slightly invalid date value, I had the following result:

    mysql> UPDATE people SET birth =”0000-09-30″ WHERE id=1234;
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    Rows matched: 1 Changed: 0 Warnings: 1

    One warning, zero rows changed! And no clue what went wrong!

    OK, time to RTFM! By reading the man page (man mysql), I found that I can enable the warnings on starting the mysql client by issuing: “mysql –show-warnings dbname” or by adding “show-warnings” in the “[mysql]” section of ~/.my.cnf configuration file. Alternatively, without changing anything, I could see the warning by executing: “show warnings;” at the console prompt.

    The warning was:

    Warning (Code 1265): Data truncated for column ‘birth’ at row 1

    which revealed nothing to me. By changing the “sql_mode” parameter to traditional

    mysql> set sql_mode=”traditional”;
    Query OK, 0 rows affected (0.06 sec)

    I had finally a clue:

    ERROR 1292 (22007): Incorrect date value: ‘0000-09-30’ for column ‘birth’ at row 1

    So, I changed again the sql_mode parameter to ALLOW_INVALID_DATES and I solved my little problem!

    Visit The Light of the LAMP blog for more…

    • Anonymous 18:17 on 2010-05-17 Permalink | Reply

      Thanks, this was really helpful. The show warnings command doesnt quite help when I am running a big SQL script.

    • Neil 12:17 on 2011-09-26 Permalink | Reply

      To anonymous above. although an old post I thought it helpful to highlight that you can output the results:

      source /path/to/whatever/big.sql > /path/to/wherever/name.txt

      This will create a log of the query results.

  • Apostolos Tsompanopoulos 20:38 on 2007-04-12 Permalink  

    Replace in place 

    What can you do when you want to replace certain text strings with their “equivalents” or “substitutes” very fast?

    Of course you can fire up your favorite text editor and start the well-known and frequently used ‘search-and-replace’ procedure.

    Or you can do the same thing using sed, like this:

    $ sed -i -e ‘s/this/that/g’ -e ‘s/foo/bar/g’ filename

    But, if you have MySQL installed, you can find the replace utility very handy, like this:

    $ replace this that foo bar — filename

    So? Except for the fewer characters typed (in the case of replace), both commands had the same result, right? Right!

    But replace can do the following in just one step:

    $ replace this that that this — filename

    or even

    $ replace foo bar bar baz baz foo — filename1 filename2 filename3

    replace is a utility written just to replace text strings and it does it very good.
    sed, on the other hand, can do a lot (and then some) more, but it’s not for someone less experienced.

    Visit The Light of the LAMP blog for more…

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc
%d bloggers like this: