HowTo - defer js for cloudflare rocket loader

Discussion in 'JavaScript' started by n00bl3t, Nov 1, 2016.

  1. #1
    Hi guys,

    I usually defer all my JS for my wordpress sites...

    however, this new template I have uses WP_ENQUEUE_SCRIPT()

    so I do not know how to modify it:

    basically;

    I would just make all my non-supported js to this:

    <script data-cfasync="false" src="/javascript.js"></script>

    but this new theme I just bought is different.

    here's what it looks like.

    /**
    * Enqueue script
    */
    function dav_enqueue_script() {
    
        wp_register_script( 'Bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4', true );
        wp_register_script( 'fotorama', get_template_directory_uri() . '/js/fotorama.js', array( 'jquery' ), '2.8.1', true );
        wp_register_script( 'countdown', get_template_directory_uri() . '/js/jquery.countdown.min.js', array( 'jquery' ), '2.0', true );
        wp_register_script( 'social', get_template_directory_uri() . '/js/social.js', array( 'jquery' ), '1.0', true );
        wp_register_script( 'stickr', get_template_directory_uri() . '/js/jquery.stickr.min.js', array( 'jquery' ), '1.0', true );
        wp_register_script( 'polyfiller', get_template_directory_uri() . '/js/polyfiller.js', array( 'jquery' ), '1.0', true );
    
        wp_register_script( 'ami3', get_template_directory_uri() . '/js/script.js', array(
            'Bootstrap',
            'fotorama',
            'countdown',
            'social',
            'front-cart',
            'front-abandoned',
            'stickr',
            'polyfiller'
        ), '1.0', true );
       
        wp_localize_script( 'ami3', 'alidAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    
        wp_localize_script( 'ami3', 'davLang',
            dav_shippingList() );
    
        wp_enqueue_script( 'ami3' );
        //todo enabled cart
        wp_enqueue_script( array( 'front-coupon', 'front-validateForm' ) );
    }
    
    add_action( 'wp_enqueue_scripts', 'dav_enqueue_script' );
    
    Code (JavaScript):
     
    n00bl3t, Nov 1, 2016 IP
  2. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #2
    Sounds like you need to use the 'script_loader_tag' tag filter and add in data-cfasync="false".
     
    Einheijar, Nov 1, 2016 IP