Selective Script/Style Loading
-
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 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Selective Script/Style Loading’ is closed to new replies.