Skip to content

Instantly share code, notes, and snippets.

@jwenerd
Last active August 29, 2015 14:01
Show Gist options
  • Save jwenerd/266c28d0a66281f4e4bf to your computer and use it in GitHub Desktop.
Save jwenerd/266c28d0a66281f4e4bf to your computer and use it in GitHub Desktop.
WP multisite upgrade script
<?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'] = 'wpdev4.tlt.psu.edu';
// 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);
define( 'WP_INSTALLING', true );
// Load WordPress Administration Bootstrap
require_once( '/var/www/wordpressmudev4/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;
global $wpdb;
$sql = "SELECT blog_id as ids FROM {$wpdb->blogs} WHERE deleted != 1 AND spam != 1";
$ids = $wpdb->get_col($sql);
$ids = array_reverse($ids);
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
remove_filter( 'wp_die_handler', '_default_wp_die_handler' );
add_filter( 'wp_die_handler', 'wp_cli_upgrade_die_handler', 100, 2 );
function print_r_dont_die(){
print_r(func_get_args());
}
function wp_cli_upgrade_die_handler(){
return 'print_r_dont_die';
}
foreach($ids as $id ){
$details = array('blog_id' => $id);
if(!switch_to_blog( $id))
continue;
$siteurl = get_option('siteurl' );
$version = get_option( 'db_version', 0);
echo "* {$id} : $siteurl : version: {$version} \n";
delete_site_transient('update_core');
wp_upgrade();
do_action( 'after_mu_upgrade');
do_action( 'wpmu_upgrade_site', $details[ 'blog_id' ] );
restore_current_blog();
/*
// Do the actual upgrade
$response = wp_remote_get( trailingslashit( $siteurl ) . "wp-admin/upgrade.php?step=
", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
$upgrade_count++;
do_action( 'after_mu_upgrade', $response );
do_action( 'wpmu_upgrade_site', $details[ 'blog_id' ] );
*/
}
$sql = "INSERT INTO `wp_sitemeta` (`meta_id` ,`site_id` ,`meta_key` ,`meta_value`) VALUES (NULL , '1', 'ms_files_rewriting', '0');";
$wpdb->query($sql);
_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