2

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?

  • 2
    @toscho has given a interesting answer on this problem field lately – Nicolai Aug 12 '14 at 7:00
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.

  • this code should me added in Parent's function.php if i'm right... I'm looking for a way to avoid touch parent's theme because on first update probably this will be rewritten... – fromthestone Aug 12 '14 at 6:53
  • @fromthestone See my edited answer. The correct file is functions.php. I believe that's what you meant. – sakibmoon Aug 12 '14 at 7:02
  • Please apologize my ignorance. Is very long time that i "don't touch" a line of code :-( I read all posts from @toscho and you, and i'm more confused than before. Is not clear in which function.php i should write the above code. Parent or Child? – fromthestone Aug 12 '14 at 8:44
  • @fromthestone, See the edited answer. – sakibmoon Aug 12 '14 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.