• Resolved southcast

    (@southcast)


    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='https://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.

    https://www.ads-software.com/plugins/jw-player-plugin-for-wordpress/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author JW Player

    (@longtail-video)

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

    Thread Starter southcast

    (@southcast)

    @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 );
    Plugin Author JW Player

    (@longtail-video)

    Thanks for sharing, glad you got it working. ??

    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. ???

    Thread Starter southcast

    (@southcast)

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

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

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

    Thread Starter southcast

    (@southcast)

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

    Plugin Author JW Player

    (@longtail-video)

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

    @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.

    Plugin Author JW Player

    (@longtail-video)

    Yes, it can be a simple fix to add.

    Thread Starter southcast

    (@southcast)

    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.

    Plugin Author JW Player

    (@longtail-video)

    Thanks for sharing that.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Handles to remove script calls from the pages that do not need JWPlayer.’ is closed to new replies.