• Hi everybody,

    I’ve got an issue regarding to scripts loaded with WordPress functions.php…
    Two scripts (masonry and imagesloaded) don’t load on homepage, whatever I do, but they load everywhere else….

    In my functions.php, I’ve got this :

    function glasgow_scripts() {
    
    	wp_enqueue_script( 'jquery' );
    
    	wp_enqueue_script( 'masonry', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array(), _S_VERSION, true );
    
    	wp_enqueue_script( 'imagesloaded', get_template_directory_uri() . '/js/imagesloaded.pkgd.min.js', array(), _S_VERSION, true );
    
    	wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array(), false );
    
    	wp_enqueue_style( 'glasgow-style', get_stylesheet_uri() );
    
    	wp_enqueue_script( 'navigation', get_template_directory_uri() . '/js/navigation.js', array(), true );
    
    	wp_enqueue_script( 'glasgow-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );
    
    	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    		wp_enqueue_script( 'comment-reply' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'glasgow_scripts' );

    and also this (and although I fear it could create a mistake, it works really well on other sites)…

    function three_scripts() {
    	if (is_home()) {
    		wp_enqueue_script( 'three-init', get_template_directory_uri() . '/js/three-init.js', array(), false );
    		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    			wp_enqueue_script( 'comment-reply' );
    		}
    	}		
    }
    add_action( 'wp_enqueue_scripts', 'three_scripts' );
    
    function add_type_attribute($tag, $handle, $src) {
    	    // if not your script, do nothing and return original $tag
    	    if ( 'three-init' !== $handle ) {
    	        return $tag;
    	    }
    	    // change the script tag by adding type="module" and return it.
    	    $tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    	    return $tag;
    }
    add_filter('script_loader_tag', 'add_type_attribute' , 10, 3);

    As you’ll see, masonry and imagesloaded aren’t loaded, but script.js is… that makes no sense to me. Then I’ve got the “imagesloaded is not a function” error, what seems to be logical as it’s not loads on homepage.

    Does someone has a clue ?

    Thanks a lot everybody !!! ??

    • This topic was modified 4 years, 7 months ago by vincent789.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    “…is not a function” means there is a syntax error somewhere. I don’t see it in your posted code, but it’s reason enough for enqueuing to not work as expected.

    BTW, all scripts which depend on another module, like masonry is dependent upon jQuery, should have the dependencies specified when enqueuing. For example:
    wp_enqueue_script( 'masonry', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array('jquery'), _S_VERSION, true );

    When you do this, you don’t really need to enqueue “jquery”, though there is no harm in doing so.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress does not load scripts on homepage’ is closed to new replies.