Add this at the end of the file:
// setup environment
define( 'WP_ENV', 'dev' ); // options: 'dev' or 'prod'
define( 'WP_SERVER', 'local' ); // options: 'local' or 'remote'
Going to production, change it to this on the remote server in wp-config.php
:
define( 'WP_ENV', 'prod' );
define( 'WP_SERVER', 'remote' );
Set up a condition:
<?php
if ( defined('WP_ENV') && WP_ENV === 'dev' ) {
// Code only running on development
error_log('Running in development mode');
}
if ( defined('WP_ENV') && WP_ENV === 'prod' ) {
// Code only in production
// like analytics ...
}
if ( defined('WP_SERVER') && WP_SERVER === 'local' ) {
// Code voor local server
$api_url = 'http://localhost:8888/api';
} elseif ( defined('WP_SERVER') && WP_SERVER === 'remote' ) {
// Code voor remote server
$api_url = 'https://example.com/api';
}
To distinguish env and server, can be done like this:
if ( WP_ENV === 'dev' && WP_SERVER === 'local' ) {
// Development + local server
}
if ( WP_ENV === 'prod' && WP_SERVER === 'remote' ) {
// Production + remote server
}
This way, you only need to switch between dev/prod and local/remote in wp-config.php
, and the logic in your child theme can react intelligently to that.