Using a CSV File as the Source of the Find and Replace Pairs with the CLI Addon

Recently a customer asked if it was possible to use a CSV file with WP Migrate DB Pro so they could perform a find and replace on their site, there were hundreds of URLs that needed replacing.

This isn’t a feature WP Migrate DB Pro supports out of the box, but with some custom code and the CLI addon, it is possible to use a CSV file, and save yourself a lot of time!


First of all you will need to have the WP Migrate DB Pro plugin, its CLI addon, and WP CLI installed, as we will be running a WP CLI command to perform a find and replace on the site’s database.

You will also need to have the CSV with all the find and replace data in two columns, eg.


Next you will need to upload the CSV file to the site. The following code snippet assumes the file will be in your /wp-content/uploads/ directory, and called “urls.csv”, but this can be changed if you need to.

Finally, create a new file called wpmdb-cli-csv-find-replace.php in your wp-content/mu-plugins directory. If mu-plugins doesn’t exist, then create it first.

Copy the code below into that file:


function my_add_csv_find_replace_pairs( $profile ) {
    if ( 'find_replace' !== $profile['action'] ) {
        return $profile;

    $csv_filename = 'urls.csv';
    $csv_delimiter = ',';

    $uploads  = wp_upload_dir();
    $csv_path = $uploads['basedir'] . '/' . $csv_filename;

    $csv_file = fopen( $csv_path, "r" );
    if ( ! $csv_file ) {
        return $profile;

    $find    = array();
    $replace = array();

    while ( false !== ( $data = fgetcsv( $csv_file, 1000, $csv_delimiter ) ) ) {
        $find[]    = $data[0];
        $replace[] = $data[1];

    fclose( $csv_file );

    if ( ! empty( $find ) && ! empty( $replace ) ) {
        $profile['replace_old'] = array_merge( array( '' ), $find );
        $profile['replace_new'] = array_merge( array( '' ), $replace );

    return $profile;

add_filter( 'wpmdb_cli_filter_get_profile_data_from_args', 'my_add_csv_find_replace_pairs' );

The CSV filename, path and delimiter string can be changed depending on your file and where you place it.

Lastly, run the WP Migrate DB Pro CLI command to perform a find and replace. I’m using rubbish values as the arguments in the command, otherwise the command won’t run, but these will be ignored and the CSV data will be used:

wp migratedb find-replace --find="foo" --replace="bar"

Once you’ve performed the migration and you are happy the data has been replaced, you can remove the mu-plugin file.