WP-CLI search-replace Command

WP-CLI, WordPress Command Line Interface makes managing your WordPress website(s) simple if you are affluent in working through the Command Line Interface (CLI). InMotion Hosting’s WordPress Hosting includes WP-CLI to supplement managing your WordPress website(s). For example, the WP-CLI command search-replace will search for a string of data (that you specify) and replaces it with another (that you specify).

This has a lot of useful applications, such as changing the website’s URL/domain, updating a file path, or even replacing every https:// call to https:// throughout the database. In this guide, you will learn how to use the WP-CLI command search-replace to replace an old URL with a new URL and replace all https:// calls to https://.

Old URL to New URL

WP-CLI’s search-replace command helps replace the URL configured for your website, should you decide to change the domain. For example, if you built a website on https://new.example.com you can use this command to replace your database details to use https://example.com.

  1. SSH into your server as the cPanel user that owns the website you want to change the URL for.
  2. Use the cd command to switch to the document root directory of your website.

    Example:

    cd public_html/my_website

  3. Run the following command:

    wp search-replace {old URL} {new URL} --dry-run

The resulting output displays the details, including tables and how many replacements are being made, like in the example output below:

[email protected] [~/public_html]# wp search-replace https://example.com https://new.example.com --dry-run +------------------+-----------------------+--------------+------+ | Table            | Column                | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |
  1. Once you have verified the replacements to be made, you may proceed to run the following command to have the database updated accordingly:

    wp search-replace https://example.com https://new.example.com

The following output will be displayed:

[email protected] [~/public_html]# wp search-replace https://example.com https://new.example.com +------------------+-----------------------+--------------+------+ | Table            | Column                | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |

https:// to HTTPS://

In addition to installing an SSL, you may find it necessary to update your website’s https:// calls to use the secure https:// version. This will help eliminate security warnings in browsers that may be detecting your website loading resources over https:// rather than the secure https:// protocol. These warnings will prevent the “green padlock” from displaying in the address bar, indicating a secure connection to your website. However, using WP-CLI’s search-replace command, you can easily replace https:// with https:// to correct these issues.

  1. SSH into your server as the cPanel user that owns the website you want to manage.
  2. Use the cd command to switch to the document root directory of your website.

    Example:

    cd public_html/my_website

  3. Run the following command:

    wp search-replace {https://URL} {https://URL} --dry-run

The resulting output displays the details, including tables and how many replacements are being made, like in the example output below:

[email protected] [~/public_html]# wp search-replace https://example.com https://example.com --dry-run +---------------------+-----------------------+--------------+------+ | Table               | Column                | Replacements | Type | +---------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |
  1. Once you have verified the replacements to be made, you may proceed to run the following command to have the database updated accordingly:

    wp search-replace https://example.com https://example.com

The following output will be displayed:

[email protected] [~/public_html]# wp search-replace https://example.com https://example.com +---------------------+-----------------------+--------------+------+ | Table               | Column                | Replacements | Type | +---------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |

Congratulations! Now you know how to use WP-CLI‘s search-replace command to change your website from using https:// to https://.

Learn more about WP-CLI, the command-line interface for WordPress, and how to streamline your WordPress maintenance workflows!

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!