0

I'm working on a wordpress theme I added a plugin name wp-fastest-cache which execute a script tag on head section

<script src='//mysite.com/wp-content/cache/wpfc-minified/fad3/13d93index.js' type='text/javascript'></script>

I have tried lots of trick from stack overflow and google but failed.Lots of files I tried to edit like function.php, fucntion.wp-script.php etc. still stuck. I need to add async = 'async' like

<script async = 'async' src='//mysite.com/wp-content/cache/wpfc-minified/fad3/13d93index.js' type='text/javascript'></script>

I know there are lots of plugins. But I want to do it manually.

And sorry for my bad english.

Thanks,

1
  • Not getting what are you asking, but try this <script async defer src='//mysite.com/wp-content/cache/wpfc-minified/fad3/13d93index.js' type='text/javascript'></script> check here for more Commented May 3, 2017 at 12:25

2 Answers 2

2

Please use the following code inside functions.php to add async to the javascript.

if(!is_admin()){
    function defer_async_of_js ( $url ) {

        if ( strpos( $url, 'jquery.js' ) ) return "$url' ";
        if ( strpos( $url, '.js' ) ) {
            return "$url' async='async' ";
        }
        else
        {
            return $url;
        }

    }
    add_filter( 'clean_url', 'defer_async_of_js', 11, 1 );
}
Sign up to request clarification or add additional context in comments.

Comments

0
function add_defer_attribute($tag, $handle) {

    // add script handles to the array below
    $scripts_to_defer = array('gform_recaptcha');
    foreach ($scripts_to_defer as $defer_script) {
        if ($defer_script === $handle) {
            return str_replace(' src', ' async src', $tag);
        }
    }
    return $tag;
}

add_filter('script_loader_tag', 'add_defer_attribute', 10, 2);

// Place above code in function file. you can add multiple id after comma. For Ex. ('gform_recaptcha', 'another_id');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.