Title: prevent loading player scripts on posts not using player
Last modified: August 22, 2016

---

# prevent loading player scripts on posts not using player

 *  [andrew55](https://wordpress.org/support/users/andrew55/)
 * (@andrew55)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/)
 * My site is slow and I’m taking every step I can to improve page load speed. I
   noticed javascript and css for JW Player is on every page of site, even if player
   is not being used.
 * Rather than loading the javascript and css for player on every page of blog, 
   it would be great to have it conditionally load on posts/pages that only use 
   JW Player.
 * It seems that this would speed up a WP significantly, which is critical these
   days considering so many people are using mobile devices to view sites.
 * Any suggestions on how to achieve this? Maybe using enqueue script function in
   functions.php? Maybe having scripts load based on shortcode embedded in page,
   etc.
 * I’ve seen this mentioned, but have no clue on how to accomplish it myself.
 * Thanks for any suggestions.
 * [https://wordpress.org/plugins/jw-player-plugin-for-wordpress/](https://wordpress.org/plugins/jw-player-plugin-for-wordpress/)

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5512860)
 * We don’t really have a built in way to do this, I’m afraid. I don’t know if there
   is a way in WP to check for specific shortocdes, either.
 *  [tomasdev](https://wordpress.org/support/users/tomasdev/)
 * (@tomasdev)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513071)
 * I see how it can be achieved by hooking into PHP `ob_start()` and I bet there’s
   a way in WP to do it.
 * JW Player user: here is a suggested solution [http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/](http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/)
 *     ```
       add_filter('the_posts', 'conditionally_load_jwplayer'); // the_posts gets triggered before wp_head
       function conditionally_load_jwplayer($posts) {
           if (empty($posts)) return $posts;
   
           // use this flag to see if styles and scripts need to be enqueued
           $shortcode_found = false;
           foreach ($posts as $post) {
               if (stripos($post->post_content, 'jwplayer') !== false) {
                   // bingo!
                   $shortcode_found = true;
                   break;
               }
           }
   
           if (!$shortcode_found) {
               // dequeue
               remove_action('wp_enqueue_scripts', array('JWP6_Plugin', 'insert_javascript'));
               remove_action('wp_head', array('JWP6_Plugin', 'insert_license_key'));
               remove_action('wp_head', array('JWP6_Plugin', 'insert_jwp6_load_event'));
           }
   
           return $posts;
       }
       ```
   
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513073)
 * Thank you.
 *  [Roy Tanck](https://wordpress.org/support/users/roytanck/)
 * (@roytanck)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513078)
 * WordPress certainly does have a way to check wether a shortcode is present:
 * [https://codex.wordpress.org/Function_Reference/has_shortcode](https://codex.wordpress.org/Function_Reference/has_shortcode)
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513079)
 * Thanks for sharing 🙂
 *  [camdoughcat](https://wordpress.org/support/users/camdoughcat/)
 * (@camdoughcat)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513080)
 * [@tomasdev](https://wordpress.org/support/users/tomasdev/)
    Thanks for that snippet,
   I put that in my child-theme’s funcions.php file and it does indeed remove the
   jwplayer script files wherever there is not a shortcode in use with the exception
   of the Home page of my blog – there is still the reference to the cdn-hosted 
   js file in the head. Any ideas?
 * No shortcodes are in use on the home page.
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513081)
 * Which script files are being removed if the file is still loading in the head?
 *  [camdoughcat](https://wordpress.org/support/users/camdoughcat/)
 * (@camdoughcat)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513082)
 * Actually I switched that funcion out and replaced with the following snippet 
   in my child-theme’s funcions.php file. This prevents any JW scripts being loaded
   except for on single posts and pages – which is the only place on my blog that
   uses the player.
 *     ```
       //ALLOW JW PLAYER SCRIPT LOAD IN POSTS AND PAGES ONLY
       function remove_jw_print() {
           if ( (!is_single()) || ( !is_singular() ) ){
                   remove_action('wp_enqueue_scripts', array('JWP6_Plugin', 'insert_javascript'));
                   remove_action('wp_head', array('JWP6_Plugin', 'insert_license_key'));
                   remove_action('wp_head', array('JWP6_Plugin', 'insert_jwp6_load_event'));
       			wp_dequeue_script('jwplayer', JWP6_Plugin::player_url());
           }
       }
       add_action ( 'wp_head','remove_jw_print', 3 );
       ```
   
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513083)
 * Ah great, thanks for sharing this.
 *  [Roy Tanck](https://wordpress.org/support/users/roytanck/)
 * (@roytanck)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513084)
 * Camdoughcat, it should be quite easy to add `has_shortcode` to the if-statement
   in your code, so that the code is only ever included when there is an actual 
   video.
 * Imho though, this is something the plugin itself should handle.
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513085)
 * The plugin is in need of a re-factor, so this will be one of the things that 
   is addressed when that happens.
 *  [gates](https://wordpress.org/support/users/gates/)
 * (@gates)
 * [11 years ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513086)
 * I know I’ve mentioned the same issue in the past on this forum or theirs. It 
   was probably over a year ago, but it looks like the WP plugin is just a not-so-
   actively supported plugin (still no captions possible for example). Fun fact:
   in one of their older version (it was somewhere in the JW Player v5 era I guess)
   of the plugin the JS did only load on posts where the shortcode was used.
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513087)
 * It is true, the plugin is in need of a major update / re-write.
 *  [camdoughcat](https://wordpress.org/support/users/camdoughcat/)
 * (@camdoughcat)
 * [11 years ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513088)
 * @ JW Player
 * Probably asking a bit much but ‘when’ do you think that might be?
 *  Plugin Author [JW Player](https://wordpress.org/support/users/longtail-video/)
 * (@longtail-video)
 * [11 years ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513089)
 * I’m afraid I can’t give an ETA, sorry!

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘prevent loading player scripts on posts not using player’ is closed to
new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/jw-player-plugin-for-wordpress_ffffff.
   svg)
 * [JW Player for Flash & HTML5 Video](https://wordpress.org/plugins/jw-player-plugin-for-wordpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/jw-player-plugin-for-wordpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/jw-player-plugin-for-wordpress/)
 * [Active Topics](https://wordpress.org/support/plugin/jw-player-plugin-for-wordpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/jw-player-plugin-for-wordpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/jw-player-plugin-for-wordpress/reviews/)

 * 15 replies
 * 6 participants
 * Last reply from: [JW Player](https://wordpress.org/support/users/longtail-video/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/prevent-loading-player-scripts-on-posts-not-using-player/#post-5513089)
 * Status: not resolved