Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdados/3555b4ded271fba3bfe6db0dc1437339 to your computer and use it in GitHub Desktop.
Save webdados/3555b4ded271fba3bfe6db0dc1437339 to your computer and use it in GitHub Desktop.
Remove WordPress unnecessary scripts and styles that some shortcodes load on every page
<?php
add_action( 'wp_enqueue_scripts', 'my_remove_unnecessary_scripts_and_styles', PHP_INT_MAX );
function my_remove_unnecessary_scripts_and_styles() {
//Remove Mapbox styles and scripts
global $post;
if ( ! ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wp_mapbox_gl_js' ) ) ) {
wp_dequeue_style( 'mapbox_gl_js_css' );
wp_dequeue_style( 'mapbox_gl_js_geocoder_css' );
wp_dequeue_style( 'mapbox_gl_js_directions_css' );
wp_dequeue_style( 'wp-mapbox-gl-js' );
wp_dequeue_script( 'mapbox_gl_js' );
wp_dequeue_script( 'mapbox_gl_geocoder_js' );
wp_dequeue_script( 'mapbox_gl_directions_js' );
wp_dequeue_script( 'wp-mapbox-gl-js' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment