Don't ever, ever, evereverevereverever modify the PHP code on a live WordPress website. PHP code is a compiled language, which means that if there's one syntax error—a missing semicolon, an extra curly bracket, a misspelled word—your site crashes and your visitors see nothing but the dreaded White Screen of Death.
(Incidentally, here's how to fix the White Screen of Death if it happens to you, heaven forbid.)
If you're going to write any custom code for someone's website, you need to clone it first. Ideally, you should have a copy on your local environment (which is fancy developer talk for "your laptop"), a copy on the web used for testing, and then, of course, the main site (often called the "production site").
That's quite a bit of cloning you'll need to do, especially if you have numerous clients. And even though cloning a WordPress website is easy in theory, it can be a huge pain in the neck in practice. So we've put together a step-by-step method that's guaranteed to get you through the process faster and with fewer headaches.
Without further ado, let's clone a site!
You'll need to download the client's files and export their database. For that, you'll need access to their hosting account. Each hosting company runs different software, so each one will look and function a little differently, but you should see something that looks basically like Windows: a bunch of little icons that represent stuff you can do on the server.
There should be a collection of icons that deal with databases. Often the host will have them separated in a little section titled "Databases." In that section, look for an icon titled "phpMyAdmin." This is a popular piece of software that helps you interact with your databases.
In the column on the left, you'll see a list of all the databases associated with your account. Click on the database, and a list of its tables will appear in the center part of the screen. At the top toward the middle, you'll see a tab called "Export." Click it.
It'll give you radio buttons for "Quick" and "Custom." Leave "Quick" selected and click the "Go" button down below.
A file should download onto your computer. This file should end with the .sql extension. This file is commonly called a SQL dump. We're going to come back to this file later, but let's leave it where it lies and move on.
Go back to the page with all the icons. You'll find one that's typically called "File Manager." Click that one. This should open up a page that looks a bit like File Explorer on a PC or Finder on Mac. It lists folders and their contents. Scroll down the list and find a folder that's normally called "public_html."
Here's where it gets a little tricky. If the client has only one WordPress installation, you're going to see all the WordPress files in this folder. But if the user has more than one WordPress installation, usually the WordPress files of their first installation will be in this folder, but there will be one or more folders mixed in with the other folders here that will contain a whole separate WordPress installation.
Either way, you want to figure out which folder contains the installation you want to clone. Let's say there's only one installation. So you'll navigate up a level in the file structure and right click on the public_html folder. In the popup menu, choose "Compress." This is going to make a compressed file and it will place that file inside the public_html folder (just to confuse you). It may take a few minutes for your server to create this file. Once it's done, download it.
Now, depending on your host's setup, this will be a .zip file or a .tar or .tar.gz file. If it's a .tar or .tar.gz file, there's a little more involved to extracting it, and we're not going to cover that here. But feel free to research it. You're going to find a lot of options. I recommend installing Linux as a subsystem so you can use Bash to extract the files. But I digress.
If it's a .zip file, you're in luck! Just move that file into the public folder of your local host environment. For WordPress, that would have to be either MAMP, WAMP or XAMPP. I use MAMP myself because I used to work with a lot of people who used Macs (though I have always opted for a PC, myself). If you don't have one of those apps installed, go do that now.
Here's a link to get MAMP, my preferred option. Once you get it installed, the default ports to run Apache/Nginx and MySQL will be set to 8888 and 8889. It will cause fewer problems to change those to the ports WordPress expects, so change the Apache/Nginx port to 80 and the MySQL port to 3306.
MAMP typically installs itself in your C drive. So it's file path should be C:\MAMP. In MAMP, the folder where you'll need to extract the files is called htdocs (which should be at the file path C:\MAMP\htdocs). But WAMP and XAMPP will also have a public folder, possibly with a different name. Just Google to find out what it's called.
Once your files are extracted, make sure you put them all in one folder in the htdocs folder. So if your website is going to be called "My Cool Website," then the file folder should live at C:\htdocs\mycoolwebsite. You can now start MAMP (or whatever local server environment you choose) and navigate in your browser to localhost/mycoolwebsite/license.txt. If the screen fills with legal jargon, you've put the files in the right place. Good work!
If not, try typing localhost:80/mycoolwebsite/license.txt into your browser's address bar instead. Sometimes, for whatever reason, MAMP and similar programs get a little finicky about that. If that doesn't work, try replacing the word "localhost" with "127.0.0.1" so your address will be "127.0.0.1/mycoolwebsite/license.txt" or "127.0.0.1:80/mycoolwebsite/license.txt" and, if that doesn't work, try adding a "/" to the end of all these addresses.
I don't know why this works sometimes, but it does.
Now, once your files are extracted, you'll need to import the database. But first, we'll need to create an empty database into which to import our exported content. So, go to your WordPress files and open the wp-config.php file. It should be in the site's root folder. This file should have the lines:
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Keep this file open in your editor, because we'll need to copy-paste information from it soon.
Now, we need to open up phpMyAdmin not on the hosting provider's server this time, but on your local machine. Your laptop or desktop computer. You'll do this differently with each local hosting environment, with MAMP, you just need to click Open WebStart Page, and phpMyAdmin will open in your browser.
Click the Databases tab, then toward the top there's a form field that says "Database name." Go to the wp-config.php file and copy the database name from the line that says: define( 'DB_NAME', 'database_name_here' );
In this example, the database name is database_name_here. So we paste that into the field and click "Create." Instantly, phpMyAdmin will notify you that the database has been created.
Now we'll need to create a user for the database. So, in the left-side column, choose the database you want to work on. Now click the "Privileges" tab at the top, and then "Add User Account" toward the bottom.
Go back to the wp-config.php file and copy the database username and password and paste them into the appropriate fields. The host name should be "localhost." Scroll down and check mark the box next to "grant all privileges on database database_name_here." Then, next to Global privileges, "check all." Scroll down to the bottom of the page and click "Go."
Now, let's import the content we got from our client's site. Click the database we've been working on, then click the "Import" tab at the top, then click "Choose a file." Choose that SQL dump that I asked you to set aside earlier in this little adventure.
At this point, sometimes phpMyAdmin will give you an error. Most of the time, these errors related to limits set on the amount of time your CPU is allowed to work on a request, or limits on the size of the file being uploaded. Most of the time you can fix this my changing your php.ini file, but this would take too long to get into in this article.
Once the database is installed, find the table called wp_options. Click it, and you should see the first row has the website's original URL. Click Edit and change it to localhost/mycoolwebsite. The second row might also have the original URL, so change that too.
Now for the moment of truth. In theory, this is all you need to do and everything is going to work great. In practice, this rarely goes off without a hitch. So let's try it.
Go to your web browser and go to your website (localhost/mycoolwebsite in our examples). Did the website appear? If it did, awesome! Great work! If not, let's trouble shoot.
A common problem at this step is to get a white screen with big black letters that say "Database error." This probably means that the username or password you put into phpMyAdmin don't match the ones in the wp-config.php file. Maybe you made a typo. Go double check those.
Side note: Do you remember when you looked in the wp-config.php file to find out the username and password for the database? Well, in that same general area of the document, you may have noticed a line that says:
You may have thought to yourself, "Hey, the database host on my computer will be 'localhost.' Maybe I should change it to that." No, don't! It will break the site. Your thinking makes sense, but just leave it alone.
If the website shows up but looks funny, the problem is probably with a plugin—be especially suspicious of any plugin that looks like it deals with SSL, because they often force your website to add "https" to your URL, but your localhost will only be http. So, navigate to your plugin folder and change the name of it to "pluginsBAK." Then refresh your browser. Did it work? If so, yay! It's working because all the plugins are deactivated. Go change the name of the plugins folder back to normal. This means the plugins will be installed, but not activated. Now you can go into your dashboard and activate each one until you find the culprit that was breaking your website.
Sometimes your site gets up and running with the menu and top bar and footer and everything looks great, but it's giving you a 404 page not found error. Here's how to fix it.
In your browser, go to your wp-login.php file and log in. Don't go to wp-admin.php directly, because if it's giving you a 404 message, it probably won't redirect you to wp-login the way it normally would if you tried to access wp-admin before signing in.
Now that you're logged in, go to Settings>Permalinks. Normally you don't actually even have to do anything on this page. All you have to do is load the page, and all the magic happens behind the scenes. But just in case, go ahead and hit Save at the bottom of the screen. This resets your permalinks to point at your localhost instead of the live site.
If this doesn't work, you can reset the permalinks from the back end. Go into your file structure, and in the root file—the one with wp-contents in it—find the .htaccess file. Now, rename the file to ".htaccessBAK" and go back to the permalinks page. This should automatically generate a new .htaccess file. You'll be able to see it on your server, so you'll now have an .htaccessBAK and an .htaccess file.
This should do it! After you follow these steps, you should have your clone working like a charm on your local environment.