Recursive install of a plugin to WordPress blogs

Recursive install of a plugin to WordPress blogs

I’ve had occasion recently to install a plugin on over 100 existing WordPress ePortfolio sites. The users themselves were unaware of the change and did not have to do anything to activate the new plugin.

The plugin itself is fairly simple ‘Allow SWF Upload‘. This plugin overrides get_allowed_mime_types to allow Shockwave files to be uploaded.

allow swf banner

I found directly altering wp-includes/functions.php and removing the unset call for $t[‘swf’] didn’t work as WordPress put this straight back when I logged in so this plugin is necessary.

I use two Linux shell scripts, a SQL file and a folder containing the plugin itself to achieve this:

add_swf_recursor.sh this file gets a list of eportfolio folders and calls
add_swf_plugin.sh which copies over the plugin, processes a new SQL file and imports the new SQL
add_swf_plugin.sql this SQL file contains a single update statement to run against each ePortfolio.

Here’s how the scripts look:

add_swf_recursor.sh

#!/bin/bash
for line in `ls /var/www/eportfolios`;do
  ./add_swf_plugin.sh $line
done

A fairly simple start the script will call for example ./add_swf_plugin.sh joebloggs  based on the results of the ls command.

add_swf_plugin.sh

#!/bin/sh

usage ()
{
    echo "./add_swf_plugin.sh joebloggs"
}

if [ $# != 1 ]
then
    usage
    exit
fi
    if [ -d /var/www/eportfolios/$1 ]; then
        cp allow-swf-upload /var/www/eportfolios/$1/wp-content/plugins/ -r
        cp add_swf_plugin.sql $1_reset.sql
        cat $1_reset.sql | sed "s/username/${1}/" > $1_execute.sql
        mysql -usbuser -pdbpass epmanager3  < $1_execute.sql
        rm $1_execute.sql
        rm $1_reset.sql 
        echo swfplugin for user $1 added
    else
        echo no wordpress for that user
    fi
exit 0

This script is the actual meat of the operation. The function usage() is called if the script does not receive exactly one parameter and shows the correct format.

If the eportfolio folder exists the plugin folder allow-swf-upload is copied over to the plugins folder on the users ePortfolio.

add_swf_plugin.sql

UPDATE username_options 
SET option_value= 'a:3:{i:0;s:35:"allow-swf-upload/allowSwfUploa.....";}'
 WHERE option_name='active_plugins'
 LIMIT 1;

Then the file add_swf_plugin.sql is copied to joebloggs_reset.sql (shown here with the SQL statement truncated for readability).

The joebloggs_reset.sql is then fed through a sed command to change the username in the file to the required user (in this case joebloggs). This could be done in one stage but I don’t like putting the template scripts at risk hence the intermediate _reset SQL file.

Next the file joebloggs_execute.sql is actually executed against the ePortfolio database.

Finally the SQL files are deleted ready for the next call from the recursor script.

The result of this is that the plugin folder is placed in the users wordpress plugins folder and the option_value for the option ‘active plugins’  in the users options table is set to show the new plugin is actually activated even though the user didn’t have to do anything.!

plugin is active!

Leave a Reply

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

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.