|
| 1 | +wp-cli/search-replace-command |
| 2 | +============================= |
| 3 | + |
| 4 | +Search/replace strings in the database. |
| 5 | + |
| 6 | +[](https://travis-ci.org/wp-cli/search-replace-command) |
| 7 | + |
| 8 | +Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing) |
| 9 | + |
| 10 | +## Using |
| 11 | + |
| 12 | +~~~ |
| 13 | +wp search-replace <old> <new> [<table>...] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables] [--export[=<file>]] [--export_insert_size=<rows>] [--skip-columns=<columns>] [--include-columns=<columns>] [--precise] [--recurse-objects] [--verbose] [--regex] [--regex-flags=<regex-flags>] |
| 14 | +~~~ |
| 15 | + |
| 16 | +Searches through all rows in a selection of tables and replaces |
| 17 | +appearances of the first string with the second string. |
| 18 | + |
| 19 | +By default, the command uses tables registered to the $wpdb object. On |
| 20 | +multisite, this will just be the tables for the current site unless |
| 21 | +--network is specified. |
| 22 | + |
| 23 | +Search/replace intelligently handles PHP serialized data, and does not |
| 24 | +change primary key values. |
| 25 | + |
| 26 | +**OPTIONS** |
| 27 | + |
| 28 | + <old> |
| 29 | + A string to search for within the database. |
| 30 | + |
| 31 | + <new> |
| 32 | + Replace instances of the first string with this new string. |
| 33 | + |
| 34 | + [<table>...] |
| 35 | + List of database tables to restrict the replacement to. Wildcards are |
| 36 | + supported, e.g. `'wp_*options'` or `'wp_post*'`. |
| 37 | + |
| 38 | + [--dry-run] |
| 39 | + Run the entire search/replace operation and show report, but don't save |
| 40 | + changes to the database. |
| 41 | + |
| 42 | + [--network] |
| 43 | + Search/replace through all the tables registered to $wpdb in a |
| 44 | + multisite install. |
| 45 | + |
| 46 | + [--all-tables-with-prefix] |
| 47 | + Enable replacement on any tables that match the table prefix even if |
| 48 | + not registered on $wpdb. |
| 49 | + |
| 50 | + [--all-tables] |
| 51 | + Enable replacement on ALL tables in the database, regardless of the |
| 52 | + prefix, and even if not registered on $wpdb. Overrides --network |
| 53 | + and --all-tables-with-prefix. |
| 54 | + |
| 55 | + [--export[=<file>]] |
| 56 | + Write transformed data as SQL file instead of saving replacements to |
| 57 | + the database. If <file> is not supplied, will output to STDOUT. |
| 58 | + |
| 59 | + [--export_insert_size=<rows>] |
| 60 | + Define number of rows in single INSERT statement when doing SQL export. |
| 61 | + You might want to change this depending on your database configuration |
| 62 | + (e.g. if you need to do fewer queries). Default: 50 |
| 63 | + |
| 64 | + [--skip-columns=<columns>] |
| 65 | + Do not perform the replacement on specific columns. Use commas to |
| 66 | + specify multiple columns. |
| 67 | + |
| 68 | + [--include-columns=<columns>] |
| 69 | + Perform the replacement on specific columns. Use commas to |
| 70 | + specify multiple columns. |
| 71 | + |
| 72 | + [--precise] |
| 73 | + Force the use of PHP (instead of SQL) which is more thorough, |
| 74 | + but slower. |
| 75 | + |
| 76 | + [--recurse-objects] |
| 77 | + Enable recursing into objects to replace strings. Defaults to true; |
| 78 | + pass --no-recurse-objects to disable. |
| 79 | + |
| 80 | + [--verbose] |
| 81 | + Prints rows to the console as they're updated. |
| 82 | + |
| 83 | + [--regex] |
| 84 | + Runs the search using a regular expression (without delimiters). |
| 85 | + Warning: search-replace will take about 15-20x longer when using --regex. |
| 86 | + |
| 87 | + [--regex-flags=<regex-flags>] |
| 88 | + Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity). |
| 89 | + |
| 90 | +**EXAMPLES** |
| 91 | + |
| 92 | + # Search and replace but skip one column |
| 93 | + $ wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid |
| 94 | + |
| 95 | + # Run search/replace operation but dont save in database |
| 96 | + $ wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run |
| 97 | + |
| 98 | + # Run case-insensitive regex search/replace operation (slow) |
| 99 | + $ wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' --regex --regex-flags='i' |
| 100 | + |
| 101 | + # Turn your production multisite database into a local dev database |
| 102 | + $ wp search-replace --url=example.com example.com example.dev 'wp_*options' wp_blogs |
| 103 | + |
| 104 | + # Search/replace to a SQL file without transforming the database |
| 105 | + $ wp search-replace foo bar --export=database.sql |
| 106 | + |
| 107 | + # Bash script: Search/replace production to development url (multisite compatible) |
| 108 | + #!/bin/bash |
| 109 | + if $(wp --url=http://example.com core is-installed --network); then |
| 110 | + wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid |
| 111 | + else |
| 112 | + wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid |
| 113 | + fi |
| 114 | + |
| 115 | +## Installing |
| 116 | + |
| 117 | +Installing this package requires WP-CLI v0.23.0 or greater. Update to the latest stable release with `wp cli update`. |
| 118 | + |
| 119 | +Once you've done so, you can install this package with `wp package install wp-cli/search-replace-command`. |
| 120 | + |
| 121 | +## Contributing |
| 122 | + |
| 123 | +We appreciate you taking the initiative to contribute to this project. |
| 124 | + |
| 125 | +Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. |
| 126 | + |
| 127 | +### Reporting a bug |
| 128 | + |
| 129 | +Think you’ve found a bug? We’d love for you to help us get it fixed. |
| 130 | + |
| 131 | +Before you create a new issue, you should [search existing issues](https://github.com/wp-cli/search-replace-command/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version. |
| 132 | + |
| 133 | +Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/wp-cli/search-replace-command/issues/new) with the following: |
| 134 | + |
| 135 | +1. What you were doing (e.g. "When I run `wp post list`"). |
| 136 | +2. What you saw (e.g. "I see a fatal about a class being undefined."). |
| 137 | +3. What you expected to see (e.g. "I expected to see the list of posts.") |
| 138 | + |
| 139 | +Include as much detail as you can, and clear steps to reproduce if possible. |
| 140 | + |
| 141 | +### Creating a pull request |
| 142 | + |
| 143 | +Want to contribute a new feature? Please first [open a new issue](https://github.com/wp-cli/search-replace-command/issues/new) to discuss whether the feature is a good fit for the project. |
| 144 | + |
| 145 | +Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience: |
| 146 | + |
| 147 | +1. Create a feature branch for each contribution. |
| 148 | +2. Submit your pull request early for feedback. |
| 149 | +3. Include functional tests with your changes. [Read the WP-CLI documentation](https://wp-cli.org/docs/pull-requests/#functional-tests) for an introduction. |
| 150 | +4. Follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/). |
| 151 | + |
| 152 | + |
| 153 | +*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.* |
0 commit comments