• Resolved Nicscott01

    (@nicscott01)


    In case anybody wants to load the ICS script/style only on the page(s) where the shortcode exists, you can use this. Also, this snippet re-registers the script so it will load in the footer.

    
    /**
     *      Selective Queuing of ICS Calendar Script
     *
     *
     */
    
    add_action( 'wp_enqueue_scripts', function() {
           
        global $post;
        
        $ics_script = wp_scripts()->query( 'ics-calendar' );
        
        if ( !empty( $ics_script ) ) {
            
            wp_deregister_script( $ics_script->handle );
            
            if ( has_shortcode( $post->post_content, 'ics_calendar' ) ) {
    
                wp_enqueue_script( $ics_script->handle, $ics_script->src, $ics_script->deps, $ics_script->ver, true );
    
                if( isset( $ics_script->extra ) && !empty( $ics_script->extra ) ) {
    
                    foreach( $ics_script->extra as $key => $value ) {
    
                        wp_scripts()->add_data( $ics_script->handle, $key, $value );
    
                    }
    
                }
    
            
            }
            
            
        }
        
        
        
        $ics_style = wp_styles()->query( 'ics-calendar' );
        
        if ( !empty( $ics_style ) ) {
            
            if ( !has_shortcode( $post->post_content, 'ics_calendar' ) ) {
                
                wp_deregister_style( $ics_style->handle );
                
            }
            
        }
        
    
        
    }, 11 );
    
    • This topic was modified 3 years, 4 months ago by Nicscott01.
Viewing 1 replies (of 1 total)
  • Plugin Author room34

    (@room34)

    ICS Calendar version 7.9.0 will include a change that moves the JavaScript loading into the footer.

    I don’t see a huge benefit from conditionally loading the scripts and styles only on pages that include a calendar. This code adds a bit of additional PHP overhead to every page of your site, which in aggregate may negate any performance benefit you gain from not loading these asset files.

Viewing 1 replies (of 1 total)
  • The topic ‘Selective Script/Style Loading’ is closed to new replies.