Uncaught ReferenceError: Masonry is not defined

Hello @hifumi,

index.php is the main WordPress template file. So, your JS code is being called in every page that uses/inherits from index.php. This is mostly likely why you’re seeing the Reference error.

The quick fix is to modify your JS to test for Masonry first or wrap it in a try/catch.

I’ve got code samples of this in CodePen.

The long term fix is insert your inline JS only for the page you need it using the WordPress hook API.

Here’s an example.

/* Add inline JS in the footer for specific pages. */
function add_my_js_script_to_wp_footer() {
    if( is_page( array( 'about-us', 'contact', 'home' ) ) ) {
            // Masonry JS goes here.
    } // if
add_action('wp_footer', 'add_my_js_script_to_wp_footer');

However, this inline might be loaded before Masonry lib is loaded. So, you’ll see the JS error once again. That means you’ll need to use something like this when you enqueue Masonry:

wp_add_inline_script('MASONRY_HANDLE_GOES_HERE', $the_script_source_above_stuffed_in_a_variable, 'before');

Please read up on wp_add_inine_script if this is the case.


  • This reply was modified 2 days ago by mark l chaves. Reason: Typo

Thank you for your reply @mlchaves!

I’ve investigated thoroughly with my Masonry, and it seems that a code in my functions.php causes the Masonry to display in the middle with no effects or featured images displayed:

function enqueue_masonry() {
        wp_enqueue_script('jquery-masonry', true );
        wp_enqueue_script('masonry', true);
        wp_enqueue_script('imagesloaded', get_template_directory_uri() . '/js/imagesloaded.pkgd.min.js', array( 'jquery' ), '', true );
        wp_enqueue_script('infinitescroll', get_template_directory_uri() . '/js/infinite-scroll.pkgd.min.js', array( 'jquery' ), '', true );  
add_action('wp_enqueue_scripts', 'enqueue_masonry');

By removing the if(is_page('is_front_page')){ the Masonry works as intended.

I then tried the long-term fix code, but it still breaks the Masonry, similar to the code in functions.php.

Not sure how to get this Masonry JavaScript to load only on the index.php, without trying to call it on other pages. This was my previous post:

I’ve figured it out! I moved the script to index.php instead rather than placing it footer.php. No error within the console, and Masonry loads only within index.php.

Is this a good method for long-term? Any tips and advice is appreciated to what I’ve just done.

Hello @hifumi,

Sorry. I should have been more specific for the long-term fix.

I wouldn’t alter the index.php or footer.php files. These are core WordPress files that can be overwritten any time WordPress is updated.

The better approach is to use the code samples I provided in your child theme’s functions.php.

Please read up on WordPress template files, child themes, and wp_add_inine_script (to specify to load the Masonry library first before your inline).

BTW, the code I shared with you earlier already has a check to display only on certain pages. You should alter this array to add/remove for your specific pages.

if( is_page( array( 'about-us', 'contact', 'home' ) ) ) {

If you didn’t realise this earlier, I recommend brushing up on your PHP.

Happy coding 😉

Source link