WP-CLI search-replace Command

In This Tutorial:

Site URL HTTP to HTTPS

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 http:// 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 http:// 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 http://new.example.com you can use this command to replace your database details to use http://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

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

    exampl3@example.com [~/public_html]# wp search-replace http://example.com http://new.example.com --dry-run +------------------+-----------------------+--------------+------+ | Table            | Column                | Replacements | Type | +------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |

     | wpcli_commentmeta   | meta_value            | 0            | SQL  | | wpcli_comments      | comment_author        | 0            | SQL  | | wpcli_comments      | comment_author_email  | 0            | SQL  | | wpcli_comments      | comment_author_url    | 0            | SQL  | | wpcli_comments      | comment_author_IP     | 0            | SQL  | | wpcli_comments      | comment_content       | 0            | SQL  | | wpcli_comments      | comment_approved      | 0            | SQL  | | wpcli_comments      | comment_agent         | 0            | SQL  | | wpcli_comments      | comment_type          | 0            | SQL  | | wpcli_links         | link_url              | 0            | SQL  | | wpcli_links         | link_name             | 0            | SQL  | | wpcli_links         | link_image            | 0            | SQL  | | wpcli_links         | link_target           | 0            | SQL  | | wpcli_links         | link_description      | 0            | SQL  | | wpcli_links         | link_visible          | 0            | SQL  | | wpcli_links         | link_rel              | 0            | SQL  | | wpcli_links         | link_notes            | 0            | SQL  | | wpcli_links         | link_rss              | 0            | SQL  | | wpcli_options       | option_name           | 0            | SQL  | | wpcli_options       | option_value          | 4            | PHP  | | wpcli_options       | autoload              | 0            | SQL  | | wpcli_postmeta      | meta_key              | 0            | SQL  | | wpcli_postmeta      | meta_value            | 0            | PHP  | | wpcli_posts         | post_content          | 18           | SQL  | | wpcli_posts         | post_title            | 0            | SQL  | | wpcli_posts         | post_excerpt          | 0            | SQL  | | wpcli_posts         | post_status           | 0            | SQL  | | wpcli_posts         | comment_status        | 0            | SQL  | | wpcli_posts         | ping_status           | 0            | SQL  | | wpcli_posts         | post_password         | 0            | SQL  | | wpcli_posts         | post_name             | 0            | SQL  | | wpcli_posts         | to_ping               | 0            | SQL  | | wpcli_posts         | pinged                | 0            | SQL  | | wpcli_posts         | post_content_filtered | 0            | SQL  | | wpcli_posts         | guid                  | 61           | SQL  | | wpcli_posts         | post_type             | 0            | SQL  | | wpcli_posts         | post_mime_type        | 0            | SQL  | | wpcli_term_taxonomy | taxonomy              | 0            | SQL  | | wpcli_term_taxonomy | description           | 0            | SQL  | | wpcli_termmeta      | meta_key              | 0            | SQL  | | wpcli_termmeta      | meta_value            | 0            | SQL  | | wpcli_terms         | name                  | 0            | SQL  | | wpcli_terms         | slug                  | 0            | SQL  | | wpcli_usermeta      | meta_key              | 0            | SQL  | | wpcli_usermeta      | meta_value            | 0            | PHP  | | wpcli_users         | user_login            | 0            | SQL  | | wpcli_users         | user_nicename         | 0            | SQL  | | wpcli_users         | user_email            | 0            | SQL  | | wpcli_users         | user_url              | 0            | SQL  | | wpcli_users         | user_activation_key   | 0            | SQL  | | wpcli_users         | display_name          | 0            | SQL  | +------------------+-----------------------+--------------+------+ Success: 83 replacements to be made.
  5. 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 http://example.com http://new.example.com

  6. The following output will be displayed:

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

     | wpcli_commentmeta   | meta_value            | 0            | SQL  | | wpcli_comments      | comment_author        | 0            | SQL  | | wpcli_comments      | comment_author_email  | 0            | SQL  | | wpcli_comments      | comment_author_url    | 0            | SQL  | | wpcli_comments      | comment_author_IP     | 0            | SQL  | | wpcli_comments      | comment_content       | 0            | SQL  | | wpcli_comments      | comment_approved      | 0            | SQL  | | wpcli_comments      | comment_agent         | 0            | SQL  | | wpcli_comments      | comment_type          | 0            | SQL  | | wpcli_links         | link_url              | 0            | SQL  | | wpcli_links         | link_name             | 0            | SQL  | | wpcli_links         | link_image            | 0            | SQL  | | wpcli_links         | link_target           | 0            | SQL  | | wpcli_links         | link_description      | 0            | SQL  | | wpcli_links         | link_visible          | 0            | SQL  | | wpcli_links         | link_rel              | 0            | SQL  | | wpcli_links         | link_notes            | 0            | SQL  | | wpcli_links         | link_rss              | 0            | SQL  | | wpcli_options       | option_name           | 0            | SQL  | | wpcli_options       | option_value          | 4            | PHP  | | wpcli_options       | autoload              | 0            | SQL  | | wpcli_postmeta      | meta_key              | 0            | SQL  | | wpcli_postmeta      | meta_value            | 0            | PHP  | | wpcli_posts         | post_content          | 18           | SQL  | | wpcli_posts         | post_title            | 0            | SQL  | | wpcli_posts         | post_excerpt          | 0            | SQL  | | wpcli_posts         | post_status           | 0            | SQL  | | wpcli_posts         | comment_status        | 0            | SQL  | | wpcli_posts         | ping_status           | 0            | SQL  | | wpcli_posts         | post_password         | 0            | SQL  | | wpcli_posts         | post_name             | 0            | SQL  | | wpcli_posts         | to_ping               | 0            | SQL  | | wpcli_posts         | pinged                | 0            | SQL  | | wpcli_posts         | post_content_filtered | 0            | SQL  | | wpcli_posts         | guid                  | 61           | SQL  | | wpcli_posts         | post_type             | 0            | SQL  | | wpcli_posts         | post_mime_type        | 0            | SQL  | | wpcli_term_taxonomy | taxonomy              | 0            | SQL  | | wpcli_term_taxonomy | description           | 0            | SQL  | | wpcli_termmeta      | meta_key              | 0            | SQL  | | wpcli_termmeta      | meta_value            | 0            | SQL  | | wpcli_terms         | name                  | 0            | SQL  | | wpcli_terms         | slug                  | 0            | SQL  | | wpcli_usermeta      | meta_key              | 0            | SQL  | | wpcli_usermeta      | meta_value            | 0            | PHP  | | wpcli_users         | user_login            | 0            | SQL  | | wpcli_users         | user_nicename         | 0            | SQL  | | wpcli_users         | user_email            | 0            | SQL  | | wpcli_users         | user_url              | 0            | SQL  | | wpcli_users         | user_activation_key   | 0            | SQL  | | wpcli_users         | display_name          | 0            | SQL  | +------------------+-----------------------+--------------+------+ Success: Made 83 replacements.

HTTP:// to HTTPS://

In addition to installing an SSL, you may find it necessary to update your website’s http:// calls to use the secure https:// version. This will help eliminate security warnings in browsers that may be detecting your website loading resources over http:// 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 http:// 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 {http://URL} {https://URL} –dry-run

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

    exampl3@example.com [~/public_html]# wp search-replace http://example.com https://example.com --dry-run +---------------------+-----------------------+--------------+------+ | Table               | Column                | Replacements | Type | +---------------------+-----------------------+--------------+------+ | wpcli_commentmeta   | meta_key              | 0            | SQL  |

     | wpcli_commentmeta   | meta_value            | 0            | SQL  | | wpcli_comments      | comment_author        | 0            | SQL  | | wpcli_comments      | comment_author_email  | 0            | SQL  | | wpcli_comments      | comment_author_url    | 0            | SQL  | | wpcli_comments      | comment_author_IP     | 0            | SQL  | | wpcli_comments      | comment_content       | 0            | SQL  | | wpcli_comments      | comment_approved      | 0            | SQL  | | wpcli_comments      | comment_agent         | 0            | SQL  | | wpcli_comments      | comment_type          | 0            | SQL  | | wpcli_links         | link_url              | 0            | SQL  | | wpcli_links         | link_name             | 0            | SQL  | | wpcli_links         | link_image            | 0            | SQL  | | wpcli_links         | link_target           | 0            | SQL  | | wpcli_links         | link_description      | 0            | SQL  | | wpcli_links         | link_visible          | 0            | SQL  | | wpcli_links         | link_rel              | 0            | SQL  | | wpcli_links         | link_notes            | 0            | SQL  | | wpcli_links         | link_rss              | 0            | SQL  | | wpcli_options       | option_name           | 0            | SQL  | | wpcli_options       | option_value          | 2            | PHP  | | wpcli_options       | autoload              | 0            | SQL  | | wpcli_postmeta      | meta_key              | 0            | SQL  | | wpcli_postmeta      | meta_value            | 0            | SQL  | | wpcli_posts         | post_content          | 1            | SQL  | | wpcli_posts         | post_title            | 0            | SQL  | | wpcli_posts         | post_excerpt          | 0            | SQL  | | wpcli_posts         | post_status           | 0            | SQL  | | wpcli_posts         | comment_status        | 0            | SQL  | | wpcli_posts         | ping_status           | 0            | SQL  | | wpcli_posts         | post_password         | 0            | SQL  | | wpcli_posts         | post_name             | 0            | SQL  | | wpcli_posts         | to_ping               | 0            | SQL  | | wpcli_posts         | pinged                | 0            | SQL  | | wpcli_posts         | post_content_filtered | 0            | SQL  | | wpcli_posts         | guid                  | 3            | SQL  | | wpcli_posts         | post_type             | 0            | SQL  | | wpcli_posts         | post_mime_type        | 0            | SQL  | | wpcli_term_taxonomy | taxonomy              | 0            | SQL  | | wpcli_term_taxonomy | description           | 0            | SQL  | | wpcli_termmeta      | meta_key              | 0            | SQL  | | wpcli_termmeta      | meta_value            | 0            | SQL  | | wpcli_terms         | name                  | 0            | SQL  | | wpcli_terms         | slug                  | 0            | SQL  | | wpcli_usermeta      | meta_key              | 0            | SQL  | | wpcli_usermeta      | meta_value            | 0            | PHP  | | wpcli_users         | user_login            | 0            | SQL  | | wpcli_users         | user_nicename         | 0            | SQL  | | wpcli_users         | user_email            | 0            | SQL  | | wpcli_users         | user_url              | 0            | SQL  | | wpcli_users         | user_activation_key   | 0            | SQL  | | wpcli_users         | display_name          | 0            | SQL  | +---------------------+-----------------------+--------------+------+ Success: 6 replacements to be made.
  5. 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 http://example.com https://example.com

  6. The following output will be displayed:

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

     | wpcli_commentmeta   | meta_value            | 0            | SQL  | | wpcli_comments      | comment_author        | 0            | SQL  | | wpcli_comments      | comment_author_email  | 0            | SQL  | | wpcli_comments      | comment_author_url    | 0            | SQL  | | wpcli_comments      | comment_author_IP     | 0            | SQL  | | wpcli_comments      | comment_content       | 0            | SQL  | | wpcli_comments      | comment_approved      | 0            | SQL  | | wpcli_comments      | comment_agent         | 0            | SQL  | | wpcli_comments      | comment_type          | 0            | SQL  | | wpcli_links         | link_url              | 0            | SQL  | | wpcli_links         | link_name             | 0            | SQL  | | wpcli_links         | link_image            | 0            | SQL  | | wpcli_links         | link_target           | 0            | SQL  | | wpcli_links         | link_description      | 0            | SQL  | | wpcli_links         | link_visible          | 0            | SQL  | | wpcli_links         | link_rel              | 0            | SQL  | | wpcli_links         | link_notes            | 0            | SQL  | | wpcli_links         | link_rss              | 0            | SQL  | | wpcli_options       | option_name           | 0            | SQL  | | wpcli_options       | option_value          | 2            | PHP  | | wpcli_options       | autoload              | 0            | SQL  | | wpcli_postmeta      | meta_key              | 0            | SQL  | | wpcli_postmeta      | meta_value            | 0            | SQL  | | wpcli_posts         | post_content          | 1            | SQL  | | wpcli_posts         | post_title            | 0            | SQL  | | wpcli_posts         | post_excerpt          | 0            | SQL  | | wpcli_posts         | post_status           | 0            | SQL  | | wpcli_posts         | comment_status        | 0            | SQL  | | wpcli_posts         | ping_status           | 0            | SQL  | | wpcli_posts         | post_password         | 0            | SQL  | | wpcli_posts         | post_name             | 0            | SQL  | | wpcli_posts         | to_ping               | 0            | SQL  | | wpcli_posts         | pinged                | 0            | SQL  | | wpcli_posts         | post_content_filtered | 0            | SQL  | | wpcli_posts         | guid                  | 3            | SQL  | | wpcli_posts         | post_type             | 0            | SQL  | | wpcli_posts         | post_mime_type        | 0            | SQL  | | wpcli_term_taxonomy | taxonomy              | 0            | SQL  | | wpcli_term_taxonomy | description           | 0            | SQL  | | wpcli_termmeta      | meta_key              | 0            | SQL  | | wpcli_termmeta      | meta_value            | 0            | SQL  | | wpcli_terms         | name                  | 0            | SQL  | | wpcli_terms         | slug                  | 0            | SQL  | | wpcli_usermeta      | meta_key              | 0            | SQL  | | wpcli_usermeta      | meta_value            | 0            | PHP  | | wpcli_users         | user_login            | 0            | SQL  | | wpcli_users         | user_nicename         | 0            | SQL  | | wpcli_users         | user_email            | 0            | SQL  | | wpcli_users         | user_url              | 0            | SQL  | | wpcli_users         | user_activation_key   | 0            | SQL  | | wpcli_users         | display_name          | 0            | SQL  | +---------------------+-----------------------+--------------+------+ Success: Made 6 replacements.

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

Leave a Reply