wp search-replace
Searches/replaces strings in the database.
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.
'wp_*options'
or 'wp_post*'
.'wp_*options'
or 'wp_post*'
.chr(1)
.# 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
These global parameters have the same behavior across all commands and affect how WP-CLI interacts with WordPress.
Argument | Description |
---|---|
--path=<path> |
Path to the WordPress files. |
--url=<url> |
Pretend request came from given URL. In multisite, this argument is how the target site is specified. |
--ssh=[<scheme>:][<user>@]<host\|container>[:<port>][<path>] |
Perform operation against a remote server over SSH (or a container using scheme of "docker", "docker-compose", "vagrant"). |
--http=<http> |
Perform operation against a remote WordPress installation over HTTP. |
--user=<id\|login\|email> |
Set the WordPress user. |
--skip-plugins[=<plugins>] |
Skip loading all plugins, or a comma-separated list of plugins. Note: mu-plugins are still loaded. |
--skip-themes[=<themes>] |
Skip loading all themes, or a comma-separated list of themes. |
--skip-packages |
Skip loading all installed packages. |
--require=<path> |
Load PHP file before running the command (may be used more than once). |
--exec=<php-code> |
Execute PHP code before running the command (may be used more than once). |
--context=<context> |
Load WordPress in a given context. |
--[no-]color |
Whether to colorize the output. |
--debug[=<group>] |
Show all PHP errors and add verbosity to WP-CLI output. Built-in groups include: bootstrap, commandfactory, and help. |
--prompt[=<assoc>] |
Prompt the user to enter values for all command arguments, or a subset specified as comma-separated values. |
--quiet |
Suppress informational messages. |