Playing with Constants in WordPress

WordPress constants are very useful to use and can come in handy during development.

Constants, constants never change. Constants are PHP variables that can be defined but never change during runtime. They can also make life a lot easier. Let’s take a look at some of the constants that you can use during development. Here’s a quote straight from the PHP documentation:

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

If you would like to see an entire list as a reference, take a look at the Resources section at the bottom of this post.1

Syntax

Before we begin, let’s take a quick look at how PHP constants work in WordPress. Notice that constants, again, are always in uppercase.

WP_CONTENT_DIR  // no trailing slash, full paths only
WP_CONTENT_URL  // full url 

Some constants, however, can be defined in wp-config.php. For example:

define('WP_DEBUG', false);

These constants can have a value contained within them. The vale can be a string (some text), a number, or a boolean value (true or false). Here’s an example with a string inside it:

define('DB_NAME', 'example_database');

General Constants

These are just several of many constants that you can use when developing a theme or plugin.

WPLANG

define('WPLANG', 'en_GB');

The WPLANG constant defines what language your WordPress is set to. You can define in the wp-config file found in the root, or main, WordPress folder. The first part of the value, is the language. In this case, en for English. After that, we have GB, which represents the country code. You can find another example below, can you guess which language and country respectively?

define ('WPLANG', 'pt_BR');

DISABLE_WP_CRON

This is useful if you would like to disable WordPress’s cron job and create your own. You can define this variable with a boolean value (true or false) in your wp-config.php file.

define ('DISABLE_WP_CRON', 'true');

WP_DEBUG

WP_DEBUG is very important for development; use it testing sites but not production or live servers. Setting this constant to true will print errors on your WordPress site.  Here’s how you define it:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG', false );

WP_DEBUG_LOG

What if you could put save all of the errors into a log? Well, thanks to WP_DEBUG_LOG, it is possible. The file will be stored inside the wp-content directory. The syntax is as follows:

define( 'WP_DEBUG_LOG', true );

Directories and Paths

These constants are very useful when developing your own themes and/or plugins. Unlike the constants shown above, these can be used outside of wp-config.php and can be placed in your theme and plugins.

ABSPATH

This is a handy constant to learn because it gives you the absolute path of your WordPress installation. Using the following command below will display the path of your root WordPress directory.

echo ABSPATH;


WP_CONTENT_DIR

WP_CONTENT_DIR will

<?php $path plugin_dir_path$file ); ?>

WP_PLUGIN_DIR

The WordPress Documentation states that you should not be using the other constants. Instead, you should be using functions. You can This constant will return your WordPress’s plugin directory. For example

<?php $path plugin_dir_path$file ); ?>

This is the recommended code to return a plugin directory:

<?php $path plugin_dir_path$file ); ?>

Resources

WP Engineer | WordPress Constants Overview

OUR SERVICE

Fan Commerce

ファンコマース

ファンとのダイレクトなコミュニケーションを実現して「感動体験」をビジネスに結びつけます。

Digital Marketing

デジタルマーケティング

未来を創造する価値ある「顧客体験」を実現する仕組みづくりを提案。