9 WordPress Defines You (probably) Didn't Know About

July 9, 2020

Here's some neat tricks for WordPress you may not be aware of. Place these defines in wp-config.php just above the line That's It. Pencils down.

1. Skip themes/plugins in core upgrades

Stop WordPress core updates from reinstalling unused, bundled themes (like twentytwenty) and plugins (like hello dolly):

define('CORE_UPGRADE_SKIP_NEW_BUNDLED', true);

2. Control core update types

Allow WordPress to perform ONLY minor updates (ie. 5.4.x -> 5.5.x) but not major versions (ie. 5.9.x -> 6.0.x):

define('WP_AUTO_UPDATE_CORE', 'minor');

...or disable all updates completely:

define('AUTOMATIC_UPDATER_DISABLED', true);

3. Support outbound connections through a proxy server (awesome for intranet sites)

define('WP_PROXY_HOST', '192.168.0.1');
define('WP_PROXY_PORT', '8000');
define('WP_PROXY_USERNAME', '');
define('WP_PROXY_PASSWORD', '');
define('WP_PROXY_BYPASS_HOSTS', 'localhost');

Leave WP_PROXY_USERNAME and WP_PROXY_PASSWORD blank unless you're network is using basic auth. If it is, just fill in the user/password in the quotes.

4. Use a variable HOME and SITEURL

Override the database's rigid HOME and SITEURL constant and allow the global host variable to be used instead:

define ('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define ('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);

This allows multiple domains to be used with the same WordPress instance. For one example, you can couple the above with these next two to use a different domain for your /wp-admin/ than your user-facing public site (more on this method here):

define ('COOKIE_DOMAIN', 'ADMIN.YOURDOMAIN.COM');
define ('WP_CONTENT_URL', "https://YOURDOMAIN.COM/wp-content");

PRO TIP: Using $_SERVER['HTTP_HOST'] may cause WP-CLI to throw errors when running commands, because the variable is not set during it's runtime. It looks like this:

PHP Notice:  Undefined index: HTTP_HOST in phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php...

It doesn't actually break anything - it's just annoying. You can solve it with a handy condition like this before the above defines (just make sure to change YOURDOMAIN.COM:

if (defined('WP_CLI') && WP_CLI) {
  $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = 'YOURDOMAIN.COM'; }

5. Control autosave interval

Prevent your database from getting bloated and set the total number of allowed revisions per post/page and also define the interval at which posts/pages are snapshotted with these:

define('WP_POST_REVISIONS', 5);
define('AUTOSAVE_INTERVAL', 300);

The AUTOSAVE_INTERVAL is set in seconds so 300 = 5 minutes.

6. Set the interval to automatically empty trash (in days)

define('EMPTY_TRASH_DAYS', 2);

7. Compress all WordPress scripts into one request

Reduce the amount of requests made by WordPress by compressing and joining all scripts into one single bundle:

define('CONCATENATE_SCRIPTS', true);

8. Redirect non-existent pages instead of 404'ing

define('NOBLOGREDIRECT', 'https://YOURDOMAIN.COM/are-you-lost');

9. Set an interval for WordPress mail

define('WP_MAIL_INTERVAL', 300);

The WP_MAIL_INTERVAL is set in seconds so 300 = 5 minutes.

BONUS: List all loaded defines available on your site

print_r(@get_defined_constants());