digisavvy
5/10/2017 - 2:39 AM

Functions to boos script loading performance

Functions to boos script loading performance

add_filter( 'script_loader_tag', 'wsds_defer_scripts', 10, 3 );
function wsds_defer_scripts( $tag, $handle, $src ) {
    $id = get_the_ID();
    // The handles of the enqueued scripts we want to defer
    $defer_scripts = array(
        'jquery-waypoints',
        "fl-builder-layout-$id",
        'devicepx',
        'tmls_ba_throttle_debounce',
        'tmls_mousewheel',
        'tmls_touchSwipe',
        'tmls_transit',
        'tmls-carouFredSel',
        'tmls-testimonials-js',
        'typed-js',
        'sage/js',
        'wp-featherlight',
        'page-links-to',
        'flexnav-js'
    );

    if ( in_array( $handle, $defer_scripts ) ) {
        return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
    }

    return $tag;
}

add_filter( 'style_loader_tag', 'wsds_defer_styles', 10, 3 );
function wsds_defer_styles( $tag, $handle, $src ) {
    $id = get_the_ID();
    // The handles of the enqueued scripts we want to defer
    $defer_styles = array(
            "fl-builder-layout-$id",
            'tmls-testimonials',
            'ionicons',
            'wp-featherlight',
            'sage/css'
    );

    if ( in_array( $handle, $defer_styles ) ) {

        return "<link rel='stylesheet' href='$src' as='style' onload='this.rel='stylesheet' >";

    }

    return $tag;
}