Skip to main content
Login Join
Snippet · PHP

Define Custom Theme Constants in WordPress

Shared by Sumit Javia · May 25, 2026 · @init

13 views
Back to Snippets

This snippet defines a reusable custom theme constant for the active WordPress theme directory URL. It helps developers access the theme path globally throughout the theme or plugin files without repeatedly calling WordPress functions.

/**
 * Define custom theme constants.
 *
 * This snippet creates a reusable constant for the active
 * theme directory URI, allowing easier access to theme assets
 * such as CSS, JavaScript, images, and other files.
 *
 * Defined Constant:
 * - THEME_PATH
 */
function sj_define_theme_constants() {

    // Prevent constant redefinition.
    if ( ! defined( 'THEME_PATH' ) ) {

        define( 'THEME_PATH', get_template_directory_uri() );
    }
}

add_action( 'init', 'sj_define_theme_constants' );