Using WordPress ‘init()’ PHP function

The init() WordPress PHP function sets up constants with default values, unless the user overrides them.

Usage

init();

Parameters

  • None

More information

See WordPress Developer Resources: init()

Examples

Set up default constants

In this example, we set up default constants using the init() function.

// Set up default constants
init();

Override default constants

In this example, we override the default constants by defining them before calling the init() function.

// Override default constants
define('WP_CONTENT_URL', 'http://example.com/wp-content');
define('WP_PLUGIN_URL', 'http://example.com/wp-content/plugins');

// Set up constants
init();

Check if a constant is already defined

In this example, we check if a constant is already defined before calling the init() function.

// Check if a constant is defined
if (!defined('WP_CONTENT_URL')) {
    define('WP_CONTENT_URL', 'http://example.com/wp-content');
}

// Set up constants
init();

Conditionally set constants

In this example, we set a constant based on the result of a condition before calling the init() function.

// Conditionally set constants
if (is_ssl()) {
    define('WP_CONTENT_URL', 'https://example.com/wp-content');
} else {
    define('WP_CONTENT_URL', 'http://example.com/wp-content');
}

// Set up constants
init();

Set constants using environment variables

In this example, we set constants using environment variables before calling the init() function.

// Set constants using environment variables
define('WP_CONTENT_URL', getenv('WP_CONTENT_URL') ?: 'http://example.com/wp-content');

// Set up constants
init();