Override Constants in Child theme

I have a parent theme that declare these constant in function.php:

define("THEME_DIR", get_template_directory());
define("THEME_DIR_URI", get_template_directory_uri());
define("THEME_NAME", "BARNELLI");
define("THEME_STYLES", THEME_DIR_URI . "/css");
define("THEME_INCLUDES", THEME_DIR . "/includes");
define("THEME_POST_TYPES", THEME_DIR . "/includes/post-types");
define("THEME_INCLUDES_URI", THEME_DIR_URI . "/includes");
define("THEME_FONTS", THEME_DIR_URI . "/font-awesome");

i need to modify some files included in includes directory and copy the directory structure (including files).

As i read in Codex Documentation, function.php in child theme is loaded before the same file in parent’s theme and only the files in the root on the parent theme can be overrode in child theme.

If i declare modified constant in child’s function.php will be overrode by parent’s function.php and if i copy (and modify) an included file into child theme directory will not work.

What do you suggest to solve this problem?

1 Answer
1

Constants can not be redefined that easily.

https://stackoverflow.com/questions/8465155/redefine-constants-in-php

So, the constants from child themes will be used instead of parents one. Though, there will be a warning, but that shouldn’t create any problem if you disable warning.

You should add constants like this if you don’t want any warning:

if ( !defined('CONSTANT') )
    define('CONSTANT', 'constant_value');

EDIT:

Simple answer, add constants in your Child theme’s functions.php and it will be used when you use those constants.

Broad answer, Why do you even bother to modify the CONSTANTS? CONSTANTS shouldn’t be modified. You can create your own sets of constants to use in your child theme. If I were you I would add the following in Child theme’s functions.php and use these new constants whenever necessary.

define("CHILD_THEME_DIR", get_stylesheet_directory());
define("CHILD_THEME_DIR_URI", get_stylesheet_directory_uri());
define("CHILD_THEME_NAME", "BARNELLI-CHILD");
define("CHILD_THEME_STYLES", CHILD_THEME_DIR_URI . "/css");
define("CHILD_THEME_INCLUDES", CHILD_THEME_DIR . "/includes");
define("CHILD_THEME_POST_TYPES", CHILD_THEME_DIR . "/includes/post-types");
define("CHILD_THEME_INCLUDES_URI", CHILD_THEME_DIR_URI . "/includes");
define("CHILD_THEME_FONTS", CHILD_THEME_DIR_URI . "/font-awesome");

As the parent theme is already declaring the constants, @toscho’s answer doesn’t apply to your case.

Leave a Comment