0

I'm trying to add masonry sript to my site, but wp_enqueue_script('jquery-masonry'); is not adding the script.

Instead I have to use:

wp_register_script('jquery_masonry', includes_url(). '/js/jquery/jquery.masonry.min.js', 'jquery');
wp_enqueue_script('jquery_masonry');

Why is that?

4
  • I took it because the answer was not helpful at all, sorry Commented Aug 20, 2013 at 17:42
  • @G.M. even if I'm not directly involved, I've tried the code as above and I can get it to work only with wp_register_script in wp_enqueue_scripts hook. this issue is very interesting. Commented Aug 20, 2013 at 18:14
  • I deleted my previous comment because I was not understand the question: my english is poor. Sorry, my bad. Commented Aug 20, 2013 at 18:23
  • wp_enqueue_script('jquery-masonry') works fine for me. You are certain it is not being added? You are not looking at a cached page somehow? Commented Aug 20, 2013 at 18:31

2 Answers 2

3

jQuery's Masonry is bundled with WordPress. All you should need is wp_enqueue_script('jquery-masonry'); but note that the "slug" is jquery-masonry, not jquery_masonry. And be sure your are enqueueing on the wp_enqueue_scripts hook or later.

function enqueue_masonry() {
  wp_enqueue_script('jquery-masonry');
}
add_action('wp_enqueue_scripts','enqueue_masonry');

I'd guess that your problem has something to do with when you register/enqueue because loading core scripts is pretty straightforward.

If that doesn't work, something is wrong with your site. A plugin or a theme is deregistering, perhaps. Without more information it is going to be hard to say what.

7
  • I'm not sure this answers his question, which is why does wp_enqueue_script('jquery-masonry') not work for him. Commented Aug 20, 2013 at 18:33
  • @vancoder wp_enqueue_script('jquery_masonry') don't work for him. s_ha_dum says: but note that the "slug" is 'jquery-masonry', not 'jquery_masonry' Commented Aug 20, 2013 at 18:36
  • @vancoder : see the edit. I am guessing about why it doesn't work, which is about all I can do with the information provided. Those are the only two things I can think that might go wrong, short of server level stuff. Commented Aug 20, 2013 at 18:37
  • 1
    @G.M. You mis-read the question. Commented Aug 20, 2013 at 18:40
  • @vancoder I think you mis-read the code in the question Commented Aug 20, 2013 at 19:03
2

s_ha_dum is correct, you don't need to enqueue it yourself.

That being said, the reason your script is not working is because your dependancy variable is not an array. Your code should read:

wp_register_script('jquery_masonry', includes_url(). '/js/jquery/jquery.masonry.min.js', array( 'jquery' ) );

Way down deep in the WP_Dependancies class, if the value passes is not an array, it sets it to be a blank array, therefore making it have no dependancies. (see wp_includes/class.wp_dependancies.php line 251 as of WP 3.6, if interested)

So what I assume is happening, is it's loading in the Masonry script BEFORE jQuery is loaded in, therefore causing it to do nothing.

That being said, use s_ha_dum's response, as it's already registered in WordPress, and waiting to be en-queued.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.