本文档介绍了在非公开环境下测试 WordPress 网站变更的多种方法,包括创建沙盒、隐藏测试区域和在本地计算机上安装 WordPress。这些方法旨在帮助开发者安全地进行主题开发、插件测试和网站重构,避免影响线上环境。
<style type="text/css" media="screen">
@import url('/wp-content/themes/yourtheme/style.css');
</style>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />There are times when you need to test changes to your WordPress powered site out of the public eye. Making changes to a live site could adversely affect your readers.
You have several choices.
Do this for test driving your WordPress Theme and style sheet, allowing you to develop your WordPress Theme on your computer. This limits you to only working on CSS and not using plugins and other power features of WordPress. This is best for just styling a page.
You can also close off access to your WordPress test site on your website server. This involves some familiarity with .htaccess and Apache, but it allows you to continue working on the Internet while not exposing your test site to the public.
If you are determined to put WordPress through its paces, you can install WordPress on your own computer with a few modifications. This allows you total control over the actions and capabilities of WordPress. You can still use plugins, template files, Themes, and redesign everything as if it were on the Internet without using bandwidth or suffering with slow Internet access times. We have two sets of explanation for this: Installing a New Installation on Your Computer and Installing an Existing WordPress Site. We also cover Moving WordPress Onto Your Website after you have finished developing your site on your computer.
A Sandbox is a term related to the sandbox you might have played in and built sand castles in as a child. It is a playground for working on concepts and exploring your imagination. A WordPress Sandbox is basically a copy of a generated page on a WordPress site that is saved to your hard drive for you to play with as you develop your final theme and look for your site.
WordPress uses different template files to generate different views on your site. In general, there is the front page view, the single post view, and the multi-post view, used for categories, archives, and search. For more information on the structure of WordPress Themes, see Site Architecture 1.5. As different page views use different CSS styles, at the least you need to put three page views in your sandbox following these instructions.
<style type="text/css" media="screen">
@import url('/wp-content/themes/yourtheme/style.css');
</style>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
Lastly, find all the image files and graphics associated with the style sheet, like background images, icons, bullets, or others, and copy them to your sandbox folder. Links to these items from within your style sheet should have no folders in their links, or link to a subfolder within your sandbox folder, such as:
header {
margin:5px;
padding:10px;
background:url(images/background.jpg)....
}
To test this, double click on one of the pages in that folder to view it in your browser. If the the styles and graphics are visible, it worked. If not, check the link to your style sheet.
Now, you have a sandbox to play in.
To use your sandbox, have two programs open. One is your text editor with the style.css file and the other is your browser with the page you are working on in view. Make a change in your style sheet, save it, then do a total refresh or your browser screen and look at what changes. Then repeat the process. When you are done with one page, go to the next page and check those changes and add more if necessary.
Here are a few tips for playing in your sandbox.
As you work, copy the files in the folder before making major renovations to a backup folder. Or you can use a file compression utility like a zip program to save the entire folder. If the changes you make do not work, you have recent backups to work from.
Before you begin, go through the source code file of each of the three page views in your text editor and add a comment as to where each division begins and ends. These often cross lines between template files and can be difficult to trace. Make your life easier by documenting where these begin and end.
Before making a huge change, write down what you are changing. This way you have notes to refer to when things do not work later and you are trying to trace the history of the changes.
When making significant changes to the style sheet or to the web page source code, add notes or comments to the code. This will also help you to keep track of the changes you’ve made.
Making a lot of changes at once makes it harder to find the small change that made things go out of whack. A good approach would be to take small steps and check, then make additional, small modifications and check again.
Begin by staying with the site architecture and style references already in place. If you will be releasing your Theme to the public, modifications to the template files must be seriously reconsidered and done carefully. If you are rebuilding your site for your private use, then you can make those changes to your HTML saved pages source code, then move those changes into the template files later.
When you have made all your changes and are satisfied with the results, it is time to move your sandbox back into your WordPress Theme.
If you go to another site from your blog, then your site could be discovered because of the referer in the browser. To prevent this, go to your real blog, then to another site.
This method is useful toward the end of testing as you can ask for people to test using other browsers / screen resolutions.
To hide your WordPress test folder from others, you can use the .htaccess file on an Apache web server. The .htaccess file is a file that stores server directives, instructions which tell the server what to do in specific situations. You could also use the Apache config file (httpd.conf) or other methods, but the .htaccess file can apply only to the folder in which the .htaccess file resides, and all the folders under that one, allowing you to restrict access to a specific folder.
Remember, this will only work on servers that support .htaccess. If you are unsure that your server supports .htaccess, contact your hosting provider. You may or may not be able to do this depending upon the access permissions you have with your host server. You may need their assistance. If you are running your own server, or if your hosting provider is clue-free, consult the AllowOverride documentation.
Using the .htaccess file, you need to provide instructions to tell the server to restrict or deny access to your WordPress test site. In the folder or directory in which WordPress is installed, do the following:
.htaccess.AuthUserFile /full/path/of/directory/.htpasswd AuthName "Security Area" AuthType Basic require valid-user/wordpress/ or blog.AuthUserFile.user_name is the user name for the access:htpasswd -c .htpasswd user_nameWhen you are ready to open your site to the public and remove the protection, delete the password and .htaccess files from their locations.
It is highly recommended that you remove the default ping URL to Ping-o-Matic!, otherwise your test posts will ping and your test blog will be made public though not accessible.
Use these instruction for setting up a local server environment for testing and development on a Mac.
In order for WordPress to work, it must have access to an Apache server, MySQL/MariaDB, and phpMyAdmin. Installing these separately can be painful. Luckily for us, XAMPP installs all of these with one program, allowing you to run WordPress on your computer. There are two versions of the program, Basic and Lite. The Lite version is usually adequate.
C:/xampplite or C:xampp.c:xampplite or c:xampp.htdocs directory – c:xampphtdocs.wp-config-sample.php in a text editor.// ** MySQL settings ** //
define('DB_NAME', 'wordpress'); // The name of the database
define('DB_USER', 'root'); // Your MySQL username
define('DB_PASSWORD', ''); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this
wp-config.php.IMPORTANT: It is possible to use this to actually host your blog if you have a good enough connection. If you want to do this, you MUST increase the security level. This description is NOT SECURE if you allow web access to your blog.
With the help of XAMPP, you can install WordPress directly on your computer and play with it to your heart’s content. This way, it is totally isolated from public exposure and all your mistakes are hidden. When you are ready, you can then move it onto your website, ready for all to see.

Begin by backing up your WordPress site completely, the files and the database. This will ensure that you have a good copy to fall back on, just in case.
A second backup is then required of your database, but it requires you do a little housekeeping.
As the WordPress database normally stands, there are statistics tables which contain a huge amount of data which add to the overall size of the database, and slow down the process of backing up and downloading this backup copy of your database. You do not have to clear these, but it is generally considered a good idea.
table_prefix found in your wp-config.php file. If you only have WordPress installed, then choose Select All from the left column.
Now, download your entire WordPress site to your computer. This is usually done with an FTP client program. Make sure you include all core WordPress files within your root or WordPress directory, including the index.php.
You should now have in your computer two items:
Copy the backup files again to somewhere safe on your machine so you work on a copy of the backup for the next stage.
C:xampp.C:xamppapacheconf and open the file called httpd.conf in a text editor.#LoadModule rewrite_module modules/mod_rewrite.so.mod_rewrite on).C:xampphtdocs. This will be for WordPress.wp-config.php on your WordPress install.// ** MySQL settings ** //
define('DB_NAME', 'wordpress'); // The name of the new database you made
define('DB_USER', 'root'); // keep this as is
define('DB_PASSWORD', ''); // keep this empty
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this
index.php should be in the /htdocs folder or in a subdirectory such as /htdocs/wordpress/. Write that down.xampp_start.exe or using the console program from Program Files ApacheFriendsXAMPPXAMPP Control Panel.wp-config.php file.Before you begin to import your SQL backup file, you need to change some information inside your .SQL file.
.sql backup database file you downloaded./htdocs/wordpress/, replace it with http://127.0.0.1/wordpress/.
Now it is time to import your sql file. From within the phpMyAdmin on your computer, click on your database name, then choose the SQL tab. From this screen, click Browse and find your backup files on your computer. Click Go. This can take a few minutes to import.

Once the procedure is complete, your database will be restored and will work just as it did before. If something goes wrong with this last part of the process, it could be that either your backup was corrupted in some way, or something went wrong with the database itself. Keep your backup files safe!
If everything so far has gone well, it is now time to visit your blog! In your browser, type in http://127.0.0.1/wordpress/index.php or the actual name of the folder you created for your WordPress files.
If you get a 404, check you have the right place. You do not need to put htdocs or xampp or anything else after the http://127.0.0.1/ except use your directory name.
WordPress should now function just as it did on the web. You do not need to use the built-in editor in WordPress to alter your files. Just open the files directly in a text editor and edit them as you would any other file. Refreshing your browser’s web page will then show the effects.
Coming soon – how to move your test site from your computer back live onto your host server site.