0

I want to add defer in Javascript in a WordPress Plugin, how can I add that?

/* Init AddToAny when an AddToAny WordPress Widget is selectively refreshed for customizer preview. */
jQuery( function() {
    // Short-circuit selective refresh events if not in customizer preview or pre-4.5.
    if ( 'undefined' === typeof wp || ! wp.customize || ! wp.customize.selectiveRefresh ) {
        return;
    }

    function addtoany_init() {
        if ( window.a2a ) {
            a2a.init_all( 'page' );
        }
    }

    // Init AddToAny when a partial is rendered.
    wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
        if ( placement.container ) {
            addtoany_init();
        }
    } );
} );
0

There is a filter: script_loader_tag. You can use it to find the correct script:

add_filter( 'script_loader_tag', function ( $tag, $handle ) {

if ( 'yourjsfilename.js' !== $handle )
    return $tag;

return str_replace( ' src', ' defer="defer" src', $tag );
}, 10, 2 );

Please let me know if any.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.