wp_enqueue_script with dependencies doesn’t work

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!

1 Answer
1

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);

Leave a Comment