;
SERVICE

Playing with Constants in WordPress

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

Author :  

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

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.

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

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:

General Constants

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

WPLANG

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?

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.

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:

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:

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.

WP_CONTENT_DIR

WP_CONTENT_DIR will

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

This is the recommended code to return a plugin directory:

Resources

WP Engineer | WordPress Constants Overview

SERVICE
  • sns