Suppose you want to create another wordpress site within your current wordpress domain – but inside a subdirectory. How do you do it?
In our case, we wanted a wordpress site for our kids to post to during our planning and travel, but didn’t want to give them access to our main site. We wanted them to have their own subdomain, but wanted to retain the same theme to start, but would be totally devoted to their own content.
The process, in a nutshell, is this:
0. Back everything up
1. Create a new wordpress database
2. Copy the current database into the new (clone the database)
3. Copy current wordpress files to subdirectory
4. Make changes to configuration
Let’s get started!
The very first step you should take is to backup everything in your current wordpress site. This means the database, the file system, everything. In case anything goes wrong, you want to be able to easily restore things to their former glory.
Create a new wordpress database
To do this, you’re going to need shell access to your hosting account. Use your favorite ssh client (Terminal under Mac, Putty under Windows) to log in to your web server.
The following will create a new database on your webserver:
mysqladmin create <<DB_name>> -u <<DB_user>> --password=secret
for example, you might type
mysqladmin create subwordpress -u root --password=secret
msysqladmin is the program that manages your wordpress database
create tells mysqladmin to create a new database
-u indicates that we’ll be specifying a mysql user id
DB_Name is the name of the new mysql database
DB_user is probably the root user of mysql
instead of “secret” you should use the real password for root
Copy the current database into the new
At the command prompt, type
mysqldump -u DB_user --password=secret orig_DB_name | mysql -u DB_user --password=secret clone_DB_name
for example, you might type
mysqldump -u root --password=secret wordpress | mysql -u root --password=secret subwordpress
you should see your prompt return in just a few seconds, indicating that the cloning was successful
Copy current wordpress installation to subdirectory
go to your wordpress installation directory
obviously, this will vary depending on your installation.
Once there, issue the command to create a new subdirectory:
now copy all of the files from your current wordpress installation into the subdirectory:
cp -R ./ ./subwordpress
Pay special attention to the spaces. There are exactly three spaces in this command, one between each of the terms listed below:
cp is the copy command
-R indicates that all directories should be recursively followed (copy all subdirectories)
./ means “this folder”
./subwordpress means “the folder named subwordpress in this folder”
you’ll get some errors at the end, indicating that the current directory cannot be copied into itself. Don’t worry about it. You wouldn’t want to do that, anyway.
Make changes to configuration
It’s a bad idea to use the root mysql user to access your wordpress database. Instead, we’ll use the mysql user in the current wordpress installation to manage the subdatabase as well.
Go to your new subwordpress directory and then edit wp-config.php to see the mysql user and password:
you will find (among other lines)
your name, user, and password will be different.
Now edit that first entry so it reads
This will tell the new wordpress installation to use the ‘subwordpress’ database. Next you want new subwordpress database the same user and password as the existing wordpress database. Save and exit.
From the command line, issue the command
mysql -u root -p
after entering the root mysql password, this will log you into mysql as root. Now at the mysql prompt, issue the commands to add the wordpress user to the new database
grant all on subwordpress.* to wpuser@localhost;
To exit mysql, use the command
this will add all privileges on the new database to the existing wpuser. Substitute your database username for wpuser.
Finally, we need to change the wordpress configuration details.
First, we need to change the siteurl. The easiest way to do this is to log in to phpmysql, and find the subwordpress database. The first record in the wp_options table is “siteurl”. Change this record from “http://yoursite.com” to “http://yoursite.com/subwordpress” where subwordpress is the subdirectory you’ve chosen.
You’ll also want to change the “home” record to this same value, “http://yoursite.com/subwordpress”.
You can now log in to your new subwordpress site
Go to http://yoursite.com/subwordpress/wp-login.php and login with your usual login id and password. You should create a new post to verify that everything is set up correctly – that the new posts only show up in the subdirectory, not on the main site.
Once you’ve verified that everything is hooked up correctly, now go ahead and create new posts, delete old posts (which have been carried over from the main site) and modify the site to your liking.
Congratulations; you’ve just cloned your wordpress installation in a new subdirectory!