1

I'm trying to load masonry but I'm having no luck, I've managed to load it manually but then I realised it was included in the wordpress core, so I thought oh that's handy.

I have it enqueue' (I really hate that word) like this:

function lart_front_end_scripts() {
            if (is_admin()) return;
            wp_enqueue_script('jquery', '1.10.2' );
            wp_enqueue_script('jquery-masonry', array('jquery'));
        }//End add_scripts

add_action('wp_print_scripts', array(&$this,'lart_front_end_scripts'));

Other jquery is loaded like normal, but I keep getting "masonry has no method" in the console and it's clearly not being loaded because of its absence under network - scripts console.

Help much appreciated.

Edit: I've updated my code to use enqueue_script instead of print_script.

function masonry() {
    wp_enqueue_script('jquery-masonry');
}//End add_scripts

add_action('wp_enqueue_scripts', array(&$this, 'masonry'));

Still no dice.

3
  • In your duplicate Q, you say this works on TwentyEleven but not in your theme. What theme is this and what does it has in its functions.php that would produce this?
    – brasofilo
    Oct 24, 2013 at 21:01
  • @brasofilo Yes I have wp_head(); in my header
    – UzumakiDev
    Oct 24, 2013 at 21:26
  • We're not seeing the full context of your code. Are you actually inside of a class? Where is other jquery being "loaded like normal"? Inside this callback, or elsewhere? Oct 24, 2013 at 22:53

3 Answers 3

2

Okay, so I made Underscore and Masonry a Dependency of another script and then it worked.

I stopped trying to enqueue add action core scripts and just set it as a dependency for the script that needed it.

Now, I'm not entirely sure if that is how it's supposed to work, I thought you still had to enqueue add action the script before you could set it as a dependency, the advantage of core scripts was that you just didn't have to register it.

So without enqueue add action'

wp_enqueue_script('homeJS', get_template_directory_uri() . '/js/home.js', array('jquery','underscore','jquery-masonry') );

Now all of of my required scripts work.

To clarify, only homeJS is being enqueue' with:

function enqueue_script(){ if(is_admin()) return; wp_enqueue_script()...... }

Jquery, Masonry and other core scripts have not been enqueued this way.

2
  • Could you explain the solution in more detail and perhaps post the code your used?
    – s_ha_dum
    Oct 24, 2013 at 23:01
  • @s_ha_dum updated answer.
    – UzumakiDev
    Oct 25, 2013 at 10:19
0

wp_print_scripts should not be used to enqueue styles or scripts on the front page. Use wp_enqueue_scripts instead.

Just use wp_enqueue_scripts:

add_action( 'wp_enqueue_scripts', 'myScripts');
function myScripts (){
    wp_enqueue_script('jquery', '1.10.2' );
    wp_enqueue_script('jquery-masonry', array('jquery'));
}
0

Masonry is bundled with and already registered by WordPress.

All you have to do, is to enqueue it:

function lart_front_end_scripts() {
    wp_enqueue_script('jquery-masonry');
} // function lart_front_end_scripts
add_action('wp_enqueue_scripts', array($this, 'lart_front_end_scripts'));
5
  • Hi, I've updated my question and it's still not working :/
    – UzumakiDev
    Oct 24, 2013 at 16:28
  • Yes to all the above, the function is in a class, jquery is loaded and all of my other scripts I've enqueued the same way are working. I also had trouble with with enqueue' underscore.js and ended up using a cdn instead.
    – UzumakiDev
    Oct 24, 2013 at 16:49
  • Also it's wp_enqueue_scripts with the "s" at the end right? as in add_action('wp_enqueue_scripts')
    – UzumakiDev
    Oct 24, 2013 at 16:52
  • The action hook is named wp_enqueue_scripts and the function is named wp_enqueue_script.
    – tfrommen
    Oct 24, 2013 at 16:57
  • Ah yes, thought so, but I got nothing yet. I'll try enqueue' some other core wordpress stuff.
    – UzumakiDev
    Oct 24, 2013 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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