Many hosts give customers a staging copy of their site. Customers use the copy to test updates, themes, and plugins before they change the live site. A staging copy shares code and data with the live site, so it can cause problems on the live site if it is not set up correctly.
Set the environment type
WordPress 5.5 added the wp_get_environment_type() function. Themes, plugins, and WordPress itself use it to change behavior per environment. The possible values are local, development, staging, and production. If no value is set, the type is production.
Set the type with the WP_ENVIRONMENT_TYPE constant in wp-config.php:
define( 'WP_ENVIRONMENT_TYPE', 'staging' );
You can also set a WP_ENVIRONMENT_TYPE system environment variable. If both are set, the constant has priority. An invalid value changes the type to production.
Set staging on every staging copy that you create. Do not copy this setting back to the live site when you push changes from staging to production.
Notes:
- When the type is
local, Application Passwords are available without HTTPSHTTPS Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP) and it is used for secure communication over a computer network, and is widely used on the Internet. In HTTPS, the communication protocol is encrypted using Transport Layer Security (TLS) or, formerly, Secure Sockets Layer (SSL).. Do not uselocalon a serverServer A server is a piece of computer hardware or software that provides functionality for other programs or devices. Typical servers are database servers, file servers, mail servers, print servers, web servers, game servers, and application servers. that is reachable from the internet. WP_DEVELOPMENT_MODE(added in WordPress 6.3) is a different setting. It is for developers who work on coreCore Core is the set of software required to run WordPress. The Core Team builds WordPress., a pluginPlugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory or can be cost-based plugin from a third-party., or a themeTheme A theme dictates the style and function of your WordPress website. Child Themes derive from the main parent theme.. It is not the same as the environment type.
Prevent side effects from the staging copy
A staging copy has the same data as the live site. Without changes, it can:
- Send emails to real customers, for example order or newsletter emails.
- Run scheduled events that call external services, for example payments, subscriptions, or APIAPI An Application Programming Interface (API) is a computing interface that defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc. syncs.
- Be indexed by search engines as duplicate content.
To prevent these problems:
- Restrict access to the copy with HTTPHTTP The Hypertext Transfer Protocol (HTTP) is a protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, where hypertext documents include hyperlinks to other resources that the user can easily access, for example by a mouse click or by tapping the screen in a web browser. authentication or another access control. You can also set the “Discourage search engines” option, but it does not restrict access, and search engines do not have to follow it:
wp option update blog_public 0 - Stop or capture outgoing email, for example with a mail catcher on the staging server.
- Disable automatic WP-CronCron Is a time-based job scheduler in Unix. Users that set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals. on the copy by setting
DISABLE_WP_CRONtotrueinwp-config.php, and disable any system cron job or external scheduler that calls itswp-cron.phpor runs its events. Review the scheduled events before you enable cron again. - Tell customers to use test or sandbox credentials for payment and other external services.
Change the URLs
The copy needs its own URLURL A specific web address of a website or web page on the Internet, such as a website’s URL www.wordpress.org. WordPress stores URLs in the databaseDatabase A database is an organized collection of data. Access to this data is usually provided by a “database management system” (DBMS) consisting of an integrated set of computer software that allows users to interact with one or more databases and provides access to all of the data contained in the database. Because of the close relationship between them, the term “database” is often used casually to refer to both a database and the DBMS used to manipulate it., also inside serialized data. Do not change the URLs with plain SQL REPLACE statements, because this breaks serialized data. Use wp search-replace, which handles serialized data:
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables-with-prefix --dry-run
Read the result of the --dry-run command first. Then run the command again without --dry-run.
Push changes to production
When customers move changes from staging to production, the live site can lose new data. Orders, comments, and new users that the live site received after the copy was made are not in the staging database.
- Prefer to push files (themes and plugins) only, and not the full database.
- If you push database tables, make a backupBackup A backup is a copy of computer data taken and stored elsewhere so that it may be used to restore the original after a data loss event. Backups can be used to recover data after its loss from data deletion or corruption, or to recover data from an earlier time. Backups provide a simple form of disaster recovery. of the live site first.
- Tell customers which data the push replaces.
More information
wp_get_environment_type()(Code Reference)- WordPress environment types (Make WordPress Core)
- Configuring development mode in 6.3 (Make WordPress Core)