WP-CLI: Require confirmation for search-replace

Worried about accidentally running a real search-replace command? Here’s how you can require confirmation for live-runs:

In your config file, add this:

require:
  - /path/to/search-replace-confirmation.php

In /path/to/search-replace-confirmation.php, add this:

<?php

$configurator = \WP_CLI::get_configurator();
$argv = array_slice( $GLOBALS['argv'], 1 );
list( $args, $assoc_args, $runtime_config ) = $configurator->parse_args( $argv );

if ( 'help' === $args[0] ) {
    // no need to run any checks for help commands
    return;
}

if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {
    // no need to run any checks if we're already dry-running
    return;
}

if ( 'search-replace' === $args[0] ) {
        WP_CLI::confirm( 'Proceed with live run?' );
}

Leave a Reply

Your email address will not be published.

%d bloggers like this: