With two defines and a plugin you can work on a staging site without editing the database or copying uploads.
When making changes to a site it’s safest to edit a staging site instead of the live site. Some hosting services include the ability to create a staging site but for those that don’t, here’s another option.
Copy the files
As the the post title says, we don’t need to copy the uploads – by that I mean files uploaded to wp-content/uploads
. After copying WordPress files, plugins and themes to my staging environment (which may be local, on the same hosting area as the live site or somewhere different), I use Bill Erickson’s brilliantly simple BE Media from Production plugin (on GitHub).
Media from live site
Bill’s plugin filters calls related to media and returns a url pointing to the live site.
The easiest way to configure the plugin is to add the following constant to wp-config.php
:
define( 'BE_MEDIA_FROM_PRODUCTION_URL', 'https://www.livesite.com' );
The plugin has other options to source some uploads from the staging site (which Bill says he does if changing image sizes and wants the first few pages of archives looking correct).
Override siteurl and home in wp_options table
If I develop a new site on a staging environment and then move it to the live server I always use the interconnect Database Search and Replace script to replace all the staging urls with the live urls.
I could do this after copying a live database to use on a staging site, and I often do, but sometimes I forget. To help with this you can override siteurl
and home
with two lines in the staging site’s wp-config.php
(as described in Editing wp-config.php):
define( 'WP_SITEURL', 'https://stagingsite.com' );
define( 'WP_HOME', 'https://stagingsite.com' );
This won’t change links within page or post content but can be sufficient if I’m only making some small functional changes.
Leave a Reply