Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Command Line script to upgrade a WordPress Multisite instance
<?php
/**
* WordPress Multisite upgrade script
*
* IMPORTANT: While a simple safeguard has been added to the script,
* you'll want to add another layer that prevents this script from
* being called via the browser.
*
* Usage:
* 1) Place in the root of your install (or another place, but modify the wp-load.php path to match)
* 2) Modify the $_SERVER['HTTP_HOST'] variable below
* 3) > php cli-ms-upgrade.php
*
* @author Mo Jangda batmoo@gmail.com
* @license GPL2
*/
// Modify this based on site domain
$_SERVER['HTTP_HOST'] = 'localhost';
// Script can only be called from command line
if( ! defined( 'STDIN' ) )
die( 'This script can only be called from the command line.' );
set_time_limit( 0 );
ini_set( 'display_errors', 'off' );
error_reporting(-1);
// Load WordPress Administration Bootstrap
require_once( dirname( __FILE__ ) . '/wp-load.php' );
if ( ! function_exists( 'wp' ) )
die( 'Couldn\'t load WordPress :(' );
// Kill if we're not running multisite
if ( ! is_multisite() )
die( __( 'Multisite is not enabled.' ) );
// Add the HTTP Utility class
require_once( ABSPATH . WPINC . '/http.php' );
global $wp_db_version;
update_site_option( 'wpmu_upgrade_site', $wp_db_version );
$upgrade_count = 0;
$n = 0;
do {
$blogs = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC LIMIT {$n}, 5", ARRAY_A );
foreach ( (array) $blogs as $details ) {
$siteurl = get_blog_option( $details['blog_id'], 'siteurl' );
echo "* $siteurl \n";
// Do the actual upgrade
$response = wp_remote_get( trailingslashit( $siteurl ) . "wp-admin/upgrade.php?step=upgrade_db", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
// TODO: Option to not die on fail
if ( is_wp_error( $response ) )
die( sprintf( __( '** Warning! Problem updating %1$s. Your server may not be able to connect to sites running on it. Error message: <em>%2$s</em>' ), $siteurl, $response->get_error_message() ) );
_e( "** Upgraded! \n\n" );
$upgrade_count++;
do_action( 'after_mu_upgrade', $response );
do_action( 'wpmu_upgrade_site', $details[ 'blog_id' ] );
}
$n += 5;
} while ( empty( $blogs ) );
_e( sprintf( "All done! Upgraded %d site(s). \n", $upgrade_count ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment