Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking to enable CloudFlare's RocketLoader feature that enables asynchronous loading of Javascript for my Wordpress site. The problem, however, is that there is one plugin for that I want to be loaded normally.

From a technical point of view, according to https://www.cloudflare.com/wiki/Rocket_Loader, I simply need to change any script tag I want ignored to have the data-cfasync="false" attribute as follows: <script data-cfasync="false" src="/javascript.js"></script>

Unfortunately, I can't seem to figure out how to do this for plugins that are automatically loaded by Wordpress.

Any ideas?

share|improve this question
    
Can I close your question as a duplicate of wordpress.stackexchange.com/q/38319/73? The answer there will solve your problem too. –  toscho Apr 27 '12 at 17:20
    
I just took a look at it, but I actually don't understand the solution. I may need an interpretation... –  Brideau Apr 27 '12 at 18:41
add comment

migrated from stackoverflow.com Apr 27 '12 at 17:11

This question came from our site for professional and enthusiast programmers.

1 Answer

The following solution assumes that your plugins are using wp_enqueue_script() to insert scripts into the HTML code. If they don't they are broken by design and we cannot change the script tag.

This code works like the one in this answer, in fact, it is almost a duplicate …

First make two lists of all script URIs you want be ignored or not ignored by the rocket loader script. Then fill the $optimize and the $ignore arrays in the following script with these URIs.

function rocket_loader_attributes( $url )
{
    $optimize = array (
        'http://example.com/nr1.js',
        'http://example.com/nr2.js'
    );
    $ignore = array (
        'http://example.com/nr3.js',
        'http://example.com/nr4.js'
    );

    if ( in_array( $url, $optimize ) )
    { // this will be optimized
        return "$url' data-cfasync='true";
    }

    if ( in_array( $url, $ignore ) )
    { // this will be ignored
        return "$url' data-cfasync='false";
    }

    return $url;
}
add_filter( 'clean_url', 'rocket_loader_attributes', 11, 1 );

You can create a plugin with this code or – second best option – add it to your theme's functions.php.

share|improve this answer
    
That is a brilliant solution, toscho ... and works, technically, from a javascript perspective and a WordPress perspective. For some unknown reason, however, it just doesn't work the way CloudFlare is set up. The html output looks as it should, with data-cfasync='true' showing. But without the "quotation marks", it doesn't invoke CloudFlare's rocketscript async functionality, so it remains text/javascript, not text/rocketscript. But you're definitely on to something, and perhaps something can still be done with this solution, which was brilliant. –  Jason Weber Sep 23 '13 at 6:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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