Support » Developing with WordPress » Uncaught ReferenceError: Masonry is not defined

  • Resolved hifumi

    (@hifumi)


    I have a Masonry script which isn’t in a .js, but placed in footer.php.

    I’ve made the script to be called only for the index.php, so it shouldn’t try to load anywhere else.

    When I’m on other pages, the console display this error:
    Uncaught ReferenceError: Masonry is not defined at (index):82
    Highlighting this section:
    var msnry = new Masonry('.js-masonry', {

    When I’m on index.php, it works fine without any errors, but visiting other pages that does not require the script to be loaded, the error appears in the console log.
    I know it’s not the best method to place the script in the footer.php, but it works there, whereas when I try to put it into a .js file it gives me $ is not a function.

    How do I go about this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    
    <?php
    /* 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' ) ) ) {
        ?>
            <script>
                // Masonry JS goes here.
            </script>
        <?php
        } // 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.

    Thanks!

    • This reply was modified 4 months ago by mark l chaves. Reason: Typo
    hifumi

    (@hifumi)

    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 );  
    }
    if(is_page('is_front_page')){
    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:
    https://wordpress.org/support/topic/how-do-i-load-several-javascripts-for-a-specific-page/

    hifumi

    (@hifumi)

    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 😉

    hifumi

    (@hifumi)

    Hi @mlchaves,

    I have altered the array for my specific page to index I believe and also tried is_front_page, since the page is named index.php, I don’t have any other alternative names for it, but it doesn’t seem to work.

    I think I wasn’t clear about what theme I have, but I’m not using a template/or a child-theme, I’ve created these php files from scratch. Would this still be okay leaving the code in index.php? The things I needed to work resides in index.php.

    When I updated the WordPress to the latest version, my code works fine and does not get overwritten.

    • This reply was modified 4 months ago by hifumi.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.