Skip to content

Instantly share code, notes, and snippets.

@atelierbram
Last active September 10, 2025 12:41
Show Gist options
  • Save atelierbram/e2a313b3d774f3bf53db4e22c1f4cbb1 to your computer and use it in GitHub Desktop.
Save atelierbram/e2a313b3d774f3bf53db4e22c1f4cbb1 to your computer and use it in GitHub Desktop.
Environment variables with WordPress PHP

Environment Variables - in wp-config.php

1. In wp-config.php

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' );

2. Use in child theme (like functions.php)

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';
}

Bonus: Combine both variables

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment