Wp search-replace (WP-CLI)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Example wp search-replace changing siteurl and home - Old situation
Example wp search-replace changing siteurl and home - New situation

wp search-replace replaces URLs in WordPress databases - Indispensable around site migrations:

$ wp help search-replace

NAME

  wp search-replace

DESCRIPTION

  Searches/replaces strings in the database.

SYNOPSIS

  wp search-replace <old> <new> [<table>...] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables]
  [--export[=<file>]] [--export_insert_size=<rows>] [--skip-tables=<tables>] [--skip-columns=<columns>]
  [--include-columns=<columns>] [--precise] [--recurse-objects] [--verbose] [--regex] [--regex-flags=<regex-flags>]
  [--regex-delimiter=<regex-delimiter>] [--regex-limit=<regex-limit>] [--format=<format>] [--report]
  [--report-changed-only] [--log[=<file>]] [--before_context=<num>] [--after_context=<num>]

  Searches through all rows in a selection of tables and replaces
  appearances of the first string with the second string.

  By default, the command uses tables registered to the `$wpdb` object. On
  multisite, this will just be the tables for the current site unless
  `--network` is specified.

  Search/replace intelligently handles PHP serialized data, and does not
  change primary key values.

OPTIONS

  <old>
    A string to search for within the database.

  <new>
    Replace instances of the first string with this new string.

  [<table>...]
    List of database tables to restrict the replacement to. Wildcards are
    supported, e.g. `'wp_*options'` or `'wp_post*'`.

  [--dry-run]
    Run the entire search/replace operation and show report, but don't save
    changes to the database.

  [--network]
    Search/replace through all the tables registered to $wpdb in a
    multisite install.

  [--all-tables-with-prefix]
    Enable replacement on any tables that match the table prefix even if
    not registered on $wpdb.

  [--all-tables]
    Enable replacement on ALL tables in the database, regardless of the
    prefix, and even if not registered on $wpdb. Overrides --network
    and --all-tables-with-prefix.

  [--export[=<file>]]
    Write transformed data as SQL file instead of saving replacements to
    the database. If <file> is not supplied, will output to STDOUT.

  [--export_insert_size=<rows>]
    Define number of rows in single INSERT statement when doing SQL export.
    You might want to change this depending on your database configuration
    (e.g. if you need to do fewer queries). Default: 50

  [--skip-tables=<tables>]
    Do not perform the replacement on specific tables. Use commas to
    specify multiple tables. Wildcards are supported, e.g. `'wp_*options'` or `'wp_post*'`.

  [--skip-columns=<columns>]
    Do not perform the replacement on specific columns. Use commas to
    specify multiple columns.

  [--include-columns=<columns>]
    Perform the replacement on specific columns. Use commas to
    specify multiple columns.

 [--precise]
    Force the use of PHP (instead of SQL) which is more thorough,
    but slower.

  [--recurse-objects]
    Enable recursing into objects to replace strings. Defaults to true;
    pass --no-recurse-objects to disable.

  [--verbose]
    Prints rows to the console as they're updated.

  [--regex]
    Runs the search using a regular expression (without delimiters).
    Warning: search-replace will take about 15-20x longer when using --regex.

  [--regex-flags=<regex-flags>]
    Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).

  [--regex-delimiter=<regex-delimiter>]
    The delimiter to use for the regex. It must be escaped if it appears in the search string. The default value is
    the result of `chr(1)`.

  [--regex-limit=<regex-limit>]
    The maximum possible replacements for the regex per row (or per unserialized data bit per row). Defaults to -1 (no
    limit).

  [--format=<format>]
    Render output in a particular format.
    ---
    default: table
    options:
      - table
      - count
    ---

  [--report]
    Produce report. Defaults to true.

  [--report-changed-only]
    Report changed fields only. Defaults to false, unless logging, when it defaults to true.

  [--log[=<file>]]
    Log the items changed. If <file> is not supplied or is "-", will output to STDOUT.
    Warning: causes a significant slow down, similar or worse to enabling --precise or --regex.

  [--before_context=<num>]
    For logging, number of characters to display before the old match and the new replacement. Default 40. Ignored if
    not logging.

  [--after_context=<num>]
    For logging, number of characters to display after the old match and the new replacement. Default 40. Ignored if
    not logging.

EXAMPLES

    # Search and replace but skip one column
    $ wp search-replace 'http://example.test' 'http://example.com' --skip-columns=guid

    # Run search/replace operation but dont save in database
    $ wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run

    # Run case-insensitive regex search/replace operation (slow)
    $ wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' --regex --regex-flags='i'

    # Turn your production multisite database into a local dev database
    $ wp search-replace --url=example.com example.com example.test 'wp_*options' wp_blogs

    # Search/replace to a SQL file without transforming the database
    $ wp search-replace foo bar --export=database.sql

    # Bash script: Search/replace production to development url (multisite compatible)
    #!/bin/bash
    if $(wp --url=http://example.com core is-installed --network); then
        wp search-replace --url=http://example.com 'http://example.com' 'http://example.test' --recurse-objects
        --network --skip-columns=guid --skip-tables=wp_users
    else
        wp search-replace 'http://example.com' 'http://example.test' --recurse-objects --skip-columns=guid
        --skip-tables=wp_users
    fi

Which tables & fields?

By default, not all tables are effected, but only specific tables and in specific fields. See [1] for details. An indication (using the --dry-run option of the tables and fields that this command takes action on by default:

+----------------------------------+-----------------------+--------------+------+
| Table                            | Column                | Replacements | Type |
+----------------------------------+-----------------------+--------------+------+
| wp_actionscheduler_actions       | hook                  | 0            | SQL  |
| wp_actionscheduler_actions       | status                | 0            | SQL  |
| wp_actionscheduler_actions       | args                  | 0            | SQL  |
| wp_actionscheduler_actions       | schedule              | 0            | PHP  |
| wp_actionscheduler_actions       | extended_args         | 0            | SQL  |
| wp_actionscheduler_groups        | slug                  | 0            | SQL  |
| wp_actionscheduler_logs          | message               | 0            | SQL  |
| wp_commentmeta                   | meta_key              | 0            | SQL  |
| wp_commentmeta                   | meta_value            | 0            | SQL  |
| wp_comments                      | comment_author        | 0            | SQL  |
| wp_comments                      | comment_author_email  | 0            | SQL  |
| wp_comments                      | comment_author_url    | 0            | SQL  |
| wp_comments                      | comment_author_IP     | 0            | SQL  |
| wp_comments                      | comment_content       | 0            | SQL  |
| wp_comments                      | comment_approved      | 0            | SQL  |
| wp_comments                      | comment_agent         | 0            | SQL  |
| wp_comments                      | comment_type          | 0            | SQL  |
| wp_links                         | link_url              | 0            | SQL  |
| wp_links                         | link_name             | 0            | SQL  |
| wp_links                         | link_image            | 0            | SQL  |
| wp_links                         | link_target           | 0            | SQL  |
| wp_links                         | link_description      | 0            | SQL  |
| wp_links                         | link_visible          | 0            | SQL  |
| wp_links                         | link_rel              | 0            | SQL  |
| wp_links                         | link_notes            | 0            | SQL  |
| wp_links                         | link_rss              | 0            | SQL  |
| wp_options                       | option_name           | 0            | SQL  |
| wp_options                       | option_value          | 14           | PHP  |
| wp_options                       | autoload              | 0            | SQL  |
| wp_postmeta                      | meta_key              | 0            | SQL  |
| wp_postmeta                      | meta_value            | 16167        | PHP  |
| wp_posts                         | post_content          | 5            | PHP  |
| wp_posts                         | post_title            | 0            | SQL  |
| wp_posts                         | post_excerpt          | 0            | SQL  |
| wp_posts                         | post_status           | 0            | SQL  |
| wp_posts                         | comment_status        | 0            | SQL  |
| wp_posts                         | ping_status           | 0            | SQL  |
| wp_posts                         | post_password         | 0            | SQL  |
| wp_posts                         | post_name             | 0            | SQL  |
| wp_posts                         | to_ping               | 0            | SQL  |
| wp_posts                         | pinged                | 0            | SQL  |
| wp_posts                         | post_content_filtered | 0            | SQL  |
| wp_posts                         | guid                  | 16456        | SQL  |
| wp_posts                         | post_type             | 0            | SQL  |
| wp_posts                         | post_mime_type        | 0            | SQL  |
| wp_term_taxonomy                 | taxonomy              | 0            | SQL  |
| wp_term_taxonomy                 | description           | 0            | SQL  |
| wp_termmeta                      | meta_key              | 0            | SQL  |
| wp_termmeta                      | meta_value            | 0            | SQL  |
| wp_terms                         | name                  | 0            | SQL  |
| wp_terms                         | slug                  | 0            | SQL  |
| wp_usermeta                      | meta_key              | 0            | SQL  |
| wp_usermeta                      | meta_value            | 1            | PHP  |
| wp_users                         | user_login            | 0            | SQL  |
| wp_users                         | user_nicename         | 0            | SQL  |
| wp_users                         | user_email            | 0            | SQL  |
| wp_users                         | user_url              | 0            | SQL  |
| wp_users                         | user_activation_key   | 0            | SQL  |
| wp_users                         | display_name          | 0            | SQL  |
| wp_wc_product_meta_lookup        | sku                   | 0            | SQL  |
| wp_wc_product_meta_lookup        | stock_status          | 0            | SQL  |
| wp_wc_product_meta_lookup        | tax_status            | 0            | SQL  |
| wp_wc_product_meta_lookup        | tax_class             | 0            | SQL  |
| wp_wc_tax_rate_classes           | name                  | 0            | SQL  |
| wp_wc_tax_rate_classes           | slug                  | 0            | SQL  |
| wp_woocommerce_order_itemmeta    | meta_key              | 0            | SQL  |
| wp_woocommerce_order_itemmeta    | meta_value            | 0            | PHP  |
| wp_woocommerce_payment_tokenmeta | meta_key              | 0            | SQL  |
| wp_woocommerce_payment_tokenmeta | meta_value            | 0            | SQL  |
| wp_yoast_seo_links               | url                   | 0            | SQL  |
| wp_yoast_seo_links               | type                  | 0            | SQL  |
+----------------------------------+-----------------------+--------------+------+
Success: 32643 replacements to be made.

See also