Programming Tutorials

How to Clone a WordPress Site to a Local or Staging Environment

Introduction

Cloning a live WordPress website means making a separate copy of its files and database so you can work on it without changing the production site. A local copy is useful for development, while a staging copy is useful for testing theme changes, plugin updates, redesigns and troubleshooting before anything goes live.

A complete clone needs both the WordPress files and the database. Copying only the files will not bring across your posts, pages, users and settings; importing only the database will not bring across your themes, plugins and uploaded media.

Advertisement

This guide covers a standard single-site WordPress installation. WordPress Multisite uses additional network-level settings and needs a multisite-specific migration procedure.

Safety rule: Make changes to the destination copy only. Do not run database search-and-replace commands against your live production database.

Advertisement

What you need before cloning a WordPress site

  • Administrator access to the source WordPress dashboard.
  • File access to the source server through SFTP, SSH or your hosting file manager.
  • Access to the source database through phpMyAdmin or an equivalent database tool.
  • A local development environment or a separate staging destination.
  • Enough storage for the site’s files and database export.
  • A recent, restorable backup of both the database and the files.

If you first need to prepare a Windows local environment, follow our guide to install WordPress locally using XAMPP.

Step 1: Make a complete backup of the live website

Before copying anything, create a backup set containing the database and the WordPress files. They are separate parts of a typical WordPress installation, so a database export alone is not a full-site backup.

  1. Choose a suitable time. If the website receives frequent orders, form submissions, comments or membership updates, plan the copy during a quiet period. You may need to pause writes briefly or take a final database export after copying the files so the two parts represent a consistent snapshot.
  2. Export the database. Open phpMyAdmin from your hosting control panel, select the database used by WordPress, choose Export, select SQL format and download the export. Use compression for a large database if your hosting tool offers it.
  3. Download the website files. Use SFTP, SSH or the hosting file manager to copy the WordPress installation. Include the wp-content directory (themes, plugins and uploads), wp-config.php, any .htaccess file and other site-specific files. A complete copy of the installation is the simplest starting point.
  4. Keep the backup safe. Store the database export and file copy somewhere outside the public website directory. Do not overwrite or delete the original production backup while preparing the clone.

WordPress explains why the database and files must be backed up separately in its official backup guide.

Step 2: Prepare the local or staging destination

  1. Create a new folder for the copy. For a local XAMPP installation, this is usually a folder inside the web root, such as htdocs/my-site. For hosted staging, create a separate staging directory or use the staging feature provided by your host.
  2. Create a new, empty database for the clone. Create a database user with the permissions needed by that database.
  3. Write down the destination URL. For example, a local site might use http://localhost/my-site. Use the actual URL assigned by your local environment or staging host.
  4. Check that the PHP and database versions at the destination are compatible with the site’s WordPress version, theme and plugins. Avoid combining the migration with unrelated upgrades; first get the copy working, then test upgrades separately.

Do not point the cloned site at the live database. A separate database and separate file directory help prevent accidental changes to production.

Step 3: Copy the WordPress files

  1. Copy the backed-up WordPress files into the destination folder. If you are using SFTP, transfer the files to the staging server; if you are working locally, extract or copy them into the local web root.
  2. Make sure hidden files such as .htaccess are included when your setup uses them.
  3. Keep the existing wp-content directory intact. It contains the site’s themes, plugins and uploaded files. Do not replace it with a blank folder.
  4. Keep a copy of the original wp-config.php for reference, but do not publish it or share it because it contains database connection details and other sensitive configuration.

Step 4: Import the database and configure wp-config.php

  1. Open phpMyAdmin for the destination database.
  2. Select the new, empty database and choose Import.
  3. Select the SQL export created in Step 1 and start the import. Wait for phpMyAdmin to confirm completion.
  4. Open the copied wp-config.php in a plain-text code editor. Update the database settings so they match the destination database and user.

The relevant settings look like this. Replace the example values with the credentials created for your destination:

define( 'DB_NAME', 'local_database_name' );
define( 'DB_USER', 'local_database_user' );
define( 'DB_PASSWORD', 'your_local_database_password' );
define( 'DB_HOST', 'localhost' );

Also check the $table_prefix value in wp-config.php. It must match the prefix used by the imported WordPress tables. Do not assume every installation uses wp_.

For more detail, see the official WordPress wp-config.php reference.

Step 5: Update the website URLs safely

The original version of this tutorial suggested opening the SQL export in a text editor and using Replace All to change the old domain to the local address. Do not use that method on a WordPress database. Themes, widgets and plugins can store URLs inside PHP-serialized data. A plain text replacement can change the stored string lengths and corrupt those values.

When WP-CLI is available, its search-replace command is a safer option because it understands serialized data. Run the commands from the destination copy, not from the live website.

First set the destination’s home and WordPress addresses. This example assumes the local URL is http://localhost/my-site:

wp option update home 'http://localhost/my-site'
wp option update siteurl 'http://localhost/my-site'

Next, preview the replacement. Substitute the real source and destination URLs:

wp search-replace 'https://www.example.com' 'http://localhost/my-site' --skip-columns=guid --dry-run

Review the dry-run report. If the matches are correct, run the same command without --dry-run:

wp search-replace 'https://www.example.com' 'http://localhost/my-site' --skip-columns=guid

The --skip-columns=guid option leaves WordPress post GUID values unchanged. If the original site used more than one URL form (for example, both http:// and https://, or with and without www), check each old form with a dry run and replace only the matches that belong to the source site.

WP-CLI documents the command’s serialized-data handling, dry-run option and column exclusions in its search-replace reference. If WP-CLI is not available, use a maintained migration or search-and-replace tool that explicitly supports serialized data. Take a fresh backup first and use its preview or dry-run feature when available. Do not edit the SQL dump manually.

Step 6: Secure the cloned site before testing

A clone may contain user accounts, email addresses, form entries, API keys, plugin settings and other data copied from production. Treat it as sensitive even when it runs on your own computer.

  • Restrict access. Keep a local copy on your own machine. For a hosted staging site, use the host’s access controls, password protection or HTTP authentication.
  • Prevent accidental indexing. In WordPress, open Settings → Reading and enable the option that discourages search engines from indexing the site. This is an indexing instruction, not a security barrier: a page can still be visited directly if someone has its URL. Use access protection for private staging content.
  • Disable live email. Configure a safe test mail destination or disable outbound email so test registrations, password resets and form submissions do not reach real users.
  • Use test integrations. Switch payment gateways to sandbox mode, replace production API keys and review webhooks, analytics, scheduled jobs and external services before testing.
  • Protect personal data. If the copy contains customer or member information, restrict who can access it and use anonymised test data where possible and appropriate.

WordPress documents the search-visibility setting in its Reading Settings guide. Google also explains that a noindex rule controls search appearance; it does not make a page private or inaccessible.

Step 7: Test the cloned WordPress site

Open the destination URL and work through this checklist before using the copy for development:

  • Confirm that the homepage and WordPress login open on the destination address, not the live domain.
  • Sign in to the cloned dashboard and confirm that the expected users and administration menus are present.
  • Open several posts and pages. Check that images, downloads, menus and internal links load from the correct destination.
  • Check that the active theme and required plugins are present. Test plugin features one at a time if something does not work.
  • Test forms, search, comments, widgets and any custom functionality you rely on.
  • Open Settings → Permalinks and select Save Changes if rewritten URLs return 404 errors. Keep the intended permalink structure unless you deliberately want to change it.
  • Check email, payment, webhook and scheduled-task behaviour to make sure the copy cannot trigger real production actions.
  • Review the PHP and web-server error logs if a page is blank or a plugin reports an error.

If a WordPress page returns a 404 after the move, see our guide to fixing 404 errors in WordPress.

Common cloning problems and how to fix them

Problem What to check
Error establishing a database connection Check DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, database permissions and the table prefix.
The clone redirects to the live website Check the destination’s home and siteurl options, then run a serialized-data-aware search-and-replace on the destination database.
Images or downloads are missing Confirm that the uploads directory was copied and that old media URLs were updated safely.
Posts open with 404 errors Save the Permalinks settings on the clone and check the web server’s rewrite configuration.
The dashboard or front end shows a plugin error Check the PHP error log. On the clone only, temporarily deactivate the suspected plugin and test again before reactivating plugins individually.
Test forms send real emails or payments Disable live integrations or replace them with test credentials before continuing.

Frequently asked questions

Will cloning my WordPress site change the live website?

It should not, provided that the copy uses a separate file directory and database and that you run all configuration and search-and-replace commands on the destination. Always double-check which database your tools are connected to before making changes.

Do I need to copy both the database and the files?

Yes, for a complete clone. The database stores content and settings, while the files include WordPress code, themes, plugins, uploads and other site-specific files.

Can I replace the website URL directly inside the SQL file?

A plain text replacement is not recommended. It can damage serialized data. Use WP-CLI’s search-replace command or a migration tool that supports serialized data, and make a backup first.

Does a cloned site include the original users?

The database copy includes the site’s user records. Treat the clone as sensitive, restrict access and reset or replace credentials if other people will use the environment.

Can I use this guide for WordPress Multisite?

No. This walkthrough is for a standard single-site installation. Multisite migrations involve network-specific tables, domain and path settings, and additional configuration. Follow a dedicated multisite migration procedure instead.

Conclusion

A reliable WordPress clone is more than a folder copy. Back up the database and files, create a separate destination, configure its database connection, update URLs with a serialization-aware tool and secure the copy before testing. Once the local or staging version passes your checks, you can use it to develop and troubleshoot without experimenting on the live website.

Further reading

Original screenshots retained from the earlier version

The following screenshots are retained from the original tutorial and were uploaded in July 2017. WordPress dashboards, hosting panels and theme settings may have changed since then. Use the current steps above rather than relying on old menu labels shown in these images.

RochakGuy

Hi, I'm Piyush and I'm a passionate blogger. I love sharing my insights on Rochaksite.com. I'm committed to providing practical and informative content that helps readers achieve their goals and make informed decisions. When I'm not writing, I enjoy exploring new topics and trends in Technology and indulging in my personal hobbies and interests.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button