Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I read How to add defer="defer" tag in plugin javascripts?How to add defer="defer" tag in plugin javascripts? and the code @toscho posted works good to defer certain js files or deferring ALL js files by commenting out the contact-form-7 line, but I need to defer all js except 1 or 2 files. I for sure need to exclude jquery.min because it causes the Revolution Slider plugin to stop working when it's deferred.

So how would I write a conditional in the following code to exclude certain js files? For example if I wanted to exclude jquery.min and jquery.ui.core.min

function add_defer_to_cf7( $url )
    {
        if ( // comment the following line out add 'defer' to all scripts
        FALSE === strpos( $url, 'contact-form-7' ) or
        FALSE === strpos( $url, '.js' )
        )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
    }
    add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );

I read How to add defer="defer" tag in plugin javascripts? and the code @toscho posted works good to defer certain js files or deferring ALL js files by commenting out the contact-form-7 line, but I need to defer all js except 1 or 2 files. I for sure need to exclude jquery.min because it causes the Revolution Slider plugin to stop working when it's deferred.

So how would I write a conditional in the following code to exclude certain js files? For example if I wanted to exclude jquery.min and jquery.ui.core.min

function add_defer_to_cf7( $url )
    {
        if ( // comment the following line out add 'defer' to all scripts
        FALSE === strpos( $url, 'contact-form-7' ) or
        FALSE === strpos( $url, '.js' )
        )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
    }
    add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );

I read How to add defer="defer" tag in plugin javascripts? and the code @toscho posted works good to defer certain js files or deferring ALL js files by commenting out the contact-form-7 line, but I need to defer all js except 1 or 2 files. I for sure need to exclude jquery.min because it causes the Revolution Slider plugin to stop working when it's deferred.

So how would I write a conditional in the following code to exclude certain js files? For example if I wanted to exclude jquery.min and jquery.ui.core.min

function add_defer_to_cf7( $url )
    {
        if ( // comment the following line out add 'defer' to all scripts
        FALSE === strpos( $url, 'contact-form-7' ) or
        FALSE === strpos( $url, '.js' )
        )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
    }
    add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );
Source Link
Nate M.
  • 77
  • 4
  • 11

Defer all js except certain ones in functions.php file

I read How to add defer="defer" tag in plugin javascripts? and the code @toscho posted works good to defer certain js files or deferring ALL js files by commenting out the contact-form-7 line, but I need to defer all js except 1 or 2 files. I for sure need to exclude jquery.min because it causes the Revolution Slider plugin to stop working when it's deferred.

So how would I write a conditional in the following code to exclude certain js files? For example if I wanted to exclude jquery.min and jquery.ui.core.min

function add_defer_to_cf7( $url )
    {
        if ( // comment the following line out add 'defer' to all scripts
        FALSE === strpos( $url, 'contact-form-7' ) or
        FALSE === strpos( $url, '.js' )
        )
        { // not our file
            return $url;
        }
        // Must be a ', not "!
        return "$url' defer='defer";
    }
    add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );