goiblas / Map-Block-Leaflet

Map Block Leaflet for WordPress 🗺️
https://wordpress.org/plugins/map-block-leaflet
GNU General Public License v2.0
17 stars 10 forks source link

Disable assets if block isn't in page #38

Closed ipatate closed 2 years ago

ipatate commented 2 years ago

The assets is in all pages. For the moment i use this snippet for remove assets if the block isn't in page.

/**
 * Completely remove assets from plugin block leaflet if isn't in page
 */
function disableLeafletAsset()
{
  $id = get_the_ID();
  if (!has_block('map-block-leaflet/map-block-leaflet', $id)) {
    wp_dequeue_script('lib-js-map-block-leaflet');
    wp_dequeue_script('lib-js-map-block-leaflet-cluster');
    wp_dequeue_script('js-editor-map-block-leaflet');

    wp_dequeue_style('css-editor-map-block-leaflet');
    wp_dequeue_style('lib-css-map-block-leaflet');
    wp_dequeue_style('lib-css-map-block-leaflet-cluster');
  }
}

add_action('wp_enqueue_scripts', 'disableLeafletAsset');
goiblas commented 2 years ago

Thanks for your contribution, in the past I had problems trying to exclude those assets. I will try your solution.

goiblas commented 2 years ago

Added in version 2.1.0