Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Good call ... it wasn't clear when he was defining/calling functions and constants and whether he was calling them directly or using WP hooks. –  EAMann Jan 4 '11 at 22:34
    
Thanks. I didn't know that. Thought it was the other way round... That may bring a lot of changes for me. If i take your answer as the sollution, than there's no sollution.. "init" comes after "after_theme_setup", but when every file from my child theme get's loaded first, than there seems to be no way to load any constants, even when i use a hook in my parent theme that get's loaded before (i tested this now). Is this right? –  kaiser Jan 5 '11 at 3:25
    
@kaiser you lost me there. (1) define constants in parent theme (2) define function with code that uses them in child theme (3) hook the function to appropriate hook. Constants will be perfectly available to use later, they are not relevant when simply defining function. This is common and viable practice, I don't understand why you see no solution in it. –  Rarst Jan 5 '11 at 9:39
    
Ok. Thanks. I just wanted to know if the problem lies within my specific setup and the order in which i call my functions into hooks. I just played around and re-ordered them and came to no sollution so far. So: Thanks for the explanation! Seems like i have to invest some more hours and use my helpers to investigate where the failure is in my loading order. –  kaiser Jan 5 '11 at 15:20
    
Oh yea. I marked this as the sollution. I traced all includes and found out, that they were loading too late. Current setup solves the issue + makes the functions.php of my child theme clean. Thanks again! Btw: You got a nice blog. –  kaiser Jan 13 '11 at 4:11

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.

share|improve this answer
    
Ehm... yea, just getting into Stackoverflow and still wonder how detailed people read what is written. It was just an example to make clear that the $arg for the placement in the footer is true. It wasn't not meant to be called outside wp_enqueue_script. –  kaiser Jan 5 '11 at 2:40
    
"wp_enqueue_script() always needs to be called inside a function attached to the 'init' action hook" - I know this is old, but it needs to be corrected. It's simply not true. You can call it anywhere so long as it's after init –  TheDeadMedic Mar 17 '11 at 8:46
    
Not anywhere. If it's called after wp_print_scripts, nothing will happen. Hooking to the init action, or to wp_enqueue_scripts, is far safer. Unless $footer === true, then you can hook it any time before wp_print_footer_scripts. –  EAMann Mar 17 '11 at 13:50
    
True. But the point I was making is you don't have to hook it to init. When I'm building page templates, I can simply call it before get_header(). Or in a plugin, anytime after I know init has run (but obviously before script printing). –  TheDeadMedic Mar 18 '11 at 21:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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