WordPress.org

Ready to get started?Download WordPress

Forums

JW Player for Flash & HTML5 Video
[resolved] Handles to remove script calls from the pages that do not need JWPlayer. (13 posts)

  1. southcast
    Member
    Posted 10 months ago #

    In order to optimize my site for speed, I am trying to remove unnecessary script and style calls printed to my header in view-source. This plugin also seem to load scripts on every page, including mostly the pages that might not have a use for the plugin such as ‘archives’, ‘custom-post-types’ and ‘custom-templates’ etc.

    These are two script calls printed in view-source I would like removed selectively from the pages that won't need it.

    <script type='text/javascript' src='http://p.jwpcdn.com/6/5/jwplayer.js?ver=3.6'></script>
    
    <script type="text/javascript">jwplayer.defaults = { "ph": 2 };</script>
                <script type="text/javascript">
                if (typeof(jwp6AddLoadEvent) == 'undefined') {
                    function jwp6AddLoadEvent(func) {
                        var oldonload = window.onload;
                        if (typeof window.onload != 'function') {
                            window.onload = func;
                        } else {
                            window.onload = function() {
                                if (oldonload) {
                                    oldonload();
                                }
                                func();
                            }
                        }
                    }
                }
                </script>

    I do not want to use yet another plugin for this purpose. I thought that this could be achieved with a few lines of code. I tried something that I learned here and tried in my functions.php and failed. I have tried many variations. Either I am not getting the right handle or my execution is wrong. Am a php newbie, please be kind and suggest me a correction.

    function erase_jwp() {
        remove_action('wp_enqueue_scripts', array('JWP6_Plugin', 'insert_javascript'));
    	wp_dequeue_script('jwplayer', JWP6_Plugin::player_url());
    }
    
    if( is_page() ) {
        add_action('wp_head', 'erase_jwp', 6);
    }

    P.S. : if (is_page()) {...} condition in the code is just for the start. If the handles are right it can always be set later.

    http://wordpress.org/plugins/jw-player-plugin-for-wordpress/

  2. JW Player
    Member
    Plugin Author

    Posted 10 months ago #

    What happens if you run that? Do you have an example?

  3. southcast
    Member
    Posted 10 months ago #

    @Longtail, I must have miserably failed to explain the issue after such a lengthy explanation. :( . Anyways I have figured it out after a few minutes of search and research. Now the jwplayer plugin only prints the scripts in my single default and custom post type posts as I wanted. I will leave the solution here in case someone is looking for same optimization.

    This code removes what jwplayer plugin prints in the header (you will find it in the view-source) from all the other excluded pages such as category, tag, date, months, year etc archives, search pages, custom-page-templates, standard pages etc. Feel free to modify the conditionals as you wish.

    //REMOVE JW PRINT
    function remove_jw_print() {
        if ( (!is_single()) && ( !is_singular(array( 'research','faqs' )) ) ) {
                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 );
  4. JW Player
    Member
    Plugin Author

    Posted 9 months ago #

    Thanks for sharing, glad you got it working. :)

  5. camdoughcat
    Member
    Posted 9 months ago #

    Thanks for sharing the code - exactly what I needed as well. However does anyone know the correct conditionals to use if I want the jwplayer to load ONLY on single posts and pages? I do not want the jwplayer.js, etc. loading on home, tag, ctegory, archive pages, etc. ???

  6. southcast
    Member
    Posted 9 months ago #

    try replacing the second line of code with this.
    if ( (!is_single()) || (!is_page()) ) {

  7. thibotus01
    Member
    Posted 9 months ago #

    Thanks! I think this should be added by default in the plugin.

    Also as the choice to load the player locally instead of http://p.jwpcdn.com/6/5/jwplayer.js

  8. southcast
    Member
    Posted 9 months ago #

    @thibotus01, exactly what I am thinking.
    @Longtail Video - would you please note this ?

  9. JW Player
    Member
    Plugin Author

    Posted 9 months ago #

    Yes, we can look into adding it in the future.

  10. camdoughcat
    Member
    Posted 9 months ago #

    @southcast - Thanks again - you're awesome!
    @Longtail Video - yeah you already have the ability to disable the shortcodes on certain pages within the plugin settings right.....so hopefully just an addition to disable loading the js files when they are not needed will be easy to implement.

  11. JW Player
    Member
    Plugin Author

    Posted 9 months ago #

    Yes, it can be a simple fix to add.

  12. southcast
    Member
    Posted 9 months ago #

    BTW for the guys whoever tries the above code, the code might give a blank screen specifically only on pages if the plugin is deactivated and the code remains in functions.php so remember to comment out or remove the code once you deactivate the plugin.
    cheers.

  13. JW Player
    Member
    Plugin Author

    Posted 9 months ago #

    Thanks for sharing that.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags