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.
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/
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;
}
@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.
Which script files are being removed if the file is still loading in the head?
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 );
Ah great, thanks for sharing this.
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.
The plugin is in need of a re-factor, so this will be one of the things that is addressed when that happens.
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.
It is true, the plugin is in need of a major update / re-write.
@ JW Player
Probably asking a bit much but ‘when’ do you think that might be?
I’m afraid I can’t give an ETA, sorry!