Question

I got the following setup:

  • Parent & Child Theme (Parent contains basic stuff, but no loops, style, etc.)
  • inside the functions.php of the Parent Theme i call my init.file, that cares about different stuff
  • right at the begging of the parent functions.php file (before the init happens) i call my constants.php file that defines theme version, etc.

When i now call

wp_enqueue_script($name, $path, $parent, VERSION_CONSTANT, $footer)

and set

$footer == true;

i would expect to get

a) the script loaded in my footer and

b) to get the *VERSION_CONSTANT* as my version number.

But it doesn't work.

Here is my workaround:

  • Set $footer == true;
  • pack it into a function
  • add_action that function to a hook on top

...and stupid as it is: it works like a charm and the constant value get's inserted correct.

Can anyone confirm this or has some explanation? I would expect to find the cause for this particular problem at where wp_enqueue_script get's loaded. This would explain why it works with the function (loaded later).

Thanks!

Was it helpful?

Solution

Parent theme's functions.php is loaded after that of child theme. So your constants are not available at the moment of child theme loading.

It is good practice to actually run any theme code at the hook after both themes are loaded. At the earliest at after_setup_theme hook. For enqueues is is explicitly documented that init hook should be used.

OTHER TIPS

I'd need to see your full code implementation to be sure ... but remember that wp_enqueue_script() always needs to be called inside a function attached to the 'init' action hook. Merely placing the function call inside your code will do nothing.

You need to follow the format:

// ... some code ... //
add_action('init', 'my_scripts');
// ... some code ... //


function my_scripts() {
    $footer = true;
    wp_enqueue_script($name, $path, $parent, VERSION_CONSTANT, $footer)
}

That said, I don't know why you're trying to set $footer externally when you could just as easily set it inline. And $footer == true; doesn't set the value of $footer ... you're evaluating equality.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top