Skip to content

Instantly share code, notes, and snippets.

@tannerm
Last active November 15, 2021 15:35
Show Gist options
  • Save tannerm/712b0c50db281489e3a33af2c31ec873 to your computer and use it in GitHub Desktop.
Save tannerm/712b0c50db281489e3a33af2c31ec873 to your computer and use it in GitHub Desktop.
Migrate WP CLI script
/**
*
* ## OPTIONS
*
* <environment>
* : The environment you are going to. 'local' or 'stage'. Defaults to 'local'
*
* <language>
* : The language to import the content to
*
* [--email]
* : the email to use for the admin email
*
* @synopsis <environment>
* @subcommand migrate
* @param $args
* @param $assoc_args
*
* @throws \WP_CLI\ExitException
* @since 1.0.0
*
* @author Tanner Moushey
*/
public function migrate ( $args = array(), $assoc_args = array() ) {
//
// Setup "from" replace URL
// This is typically production but can be overridden
//
$from_url = '[url of prod site]';
if ( ! empty( $assoc_args['from-url'] ) ) {
$from_url = trim( $assoc_args['from-url'] );
$from_url = rtrim( $from_url, '/' );
$from_url = esc_url( $from_url );
}
//
// Default theme and core options
// Used for Staging and Local migration
//
// General email address, typically the main tester/developer
$admin_email = '[admin email]';
// Override in wp cli command
if ( ! empty( $assoc_args['email'] ) ) {
$admin_email = trim( $assoc_args['email'] );
$admin_email = sanitize_email( $admin_email );
}
//
// Determine where the migration is going
//
$is_prod = FALSE;
if ( 'local' === $args[0] ) {
// Local install
WP_CLI::line( 'Starting local migration ...' );
$to_url = '[local url]';
} elseif ( 'stage' === $args[0] ) {
// Staging install
WP_CLI::line( 'Starting stage migration ...' );
$to_url = '[staging url]';
} else {
WP_CLI::error( 'Environment not found, exiting...' );
return;
}
//
// WP-CLI search-replace for URLs and GUIDs
//
WP_CLI::line( 'Starting WP-CLI search-replace ...' );
WP_CLI::runcommand( 'search-replace "' . $from_url . '" "' . $to_url . '"' );
if ( ! $is_prod ) {
WP_CLI::line( 'Activating dev plugins ...' );
WP_CLI::runcommand( 'plugin install --activate debug-bar wp-crontrol email-log disable-emails debug-bar-console debug-bar-cron debug-bar-extender user-switching query-monitor' );
WP_CLI::line( 'Deactivating prod plugins ...' );
WP_CLI::runcommand( 'plugin deactivate sendgrid-email-delivery-simplified --network' );
WP_CLI::runcommand( 'plugin deactivate sendgrid-email-delivery-simplified wp-mail-smtp mail-chimp-add-on-for-restrict-content-pro google-analytics-async w3-total-cache S3-Uploads backwpup wordpress-seo-premium aryo-activity-log clickfunnels wp-newrelic email-marketing clickfunnels' );
}
// Core options
// WP_CLI::line( 'Starting WP theme and core updates ...' );
update_option( 'admin_email', $admin_email );
WP_CLI::success( 'DONE' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment