2

I'm having this code:

wp_register_script('parent','parent.js', array('child'), '1', true);
wp_register_script('child', 'child.js', array('grandchild'), '1', true);
wp_register_script('grandchild', 'grandchild.js', array(), '1', true);
wp_enqueue_script('parent');

and it works fine, rendering grandchild.js, then child.js, then parent.js in footer.

Every combination of just parent and child works fine, regardless of TRUE or FALSE 'render in footer'. But when I register 'child' to be rendered in head instead:

wp_register_script('parent','parent.js', array('child'), '1', true);
wp_register_script('child', 'child.js', array('grandchild'), '1', FALSE);
wp_register_script('grandchild', 'grandchild.js', array(), '1', true);
wp_enqueue_script('parent');

Then child.js gets rendered in head, parent.js gets rendered in footer, and grandchild.js is not rendered at all ! Even though there is obviously a dependency for it.

Looks like a bug to me. Am I missing something? Shouldn't it work?

Thanks!

5
  • How can you fool WordPress rendering grandchild in footer and depended JS (child) in header ?
    – Sumit
    Commented Feb 19, 2016 at 16:00
  • 1
    Could actually be this core.trac.wordpress.org/ticket/35643 , that one will be fixed in 4.5 Commented Feb 19, 2016 at 16:02
  • Sumit, I'm not sure I understood you. Try enqueueing just two items: parent and child, play with it's "head" and "footer" settings, it's pretty good and logical. I expect multi level dependencies to be as good as 2 level dependencies. Under no circumstance I expect one of scripts I depend on to just silently never render. This is wrong. Commented Feb 19, 2016 at 16:10
  • Tried nightly build, still getting this weird behaviour. Bug I mentioned in comment above have been fixed, it's not it. Filed a new one core.trac.wordpress.org/ticket/35873 Commented Feb 19, 2016 at 22:46
  • 2
    This is related to wordpress.stackexchange.com/questions/168688/… and core.trac.wordpress.org/ticket/25247 - I'll comment on your ticket.
    – bonger
    Commented Feb 20, 2016 at 0:26

1 Answer 1

2

This is a bug in Wordpress.

https://core.trac.wordpress.org/ticket/35873

As far as I can see, it can currently be fixed with https://core.trac.wordpress.org/attachment/ticket/35873/35873.3.patch, if you are reading this some time later, it has probably already been fixed for your Wordpress version.

As a temporary workaround, set parent dependencies to both child and grandchild. This way grandchild.js will not get lost:

wp_register_script('parent','parent.js', array('grandchild', 'child'), '1', true);
wp_register_script('child', 'child.js', array('grandchild'), '1', FALSE);
wp_register_script('grandchild', 'grandchild.js', array(), '1', true);
1
  • 2
    Nice one Dima Stefantsov, thanks to you it looks like this will finally get into core.
    – bonger
    Commented Feb 20, 2016 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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