FacetWP Map facet – turn off fitBounds – for custom default lat / lng / zoom

<?php

// Add to your (child) theme's functions.php

// To be able to set a custom default center (lat / lng) and/or zoom level in the Map facet's settings, the Google maps fitBounds function needs to be turne off.
// See: https://facetwp.com/help-center/facets/facet-types/map/#set-a-custom-zoom-level-or-location-center

// There are 2 options (add only one to your site):

// 1. Only on initial page load

add_action( 'wp_head', function() {
  ?>
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        if ('undefined' !== typeof FWP && 'undefined' !== typeof FWP.hooks) {
          FWP.hooks.addFilter('facetwp_map/fit_bounds', function(fit_bounds) {
            return FWP.loaded; // force the custom lat/lng/zoom only on initial page load
          });
        }
      });
    </script>
  <?php
}, 100 );


// 2. On every page load and facet refresh:

add_action( 'wp_head', function() {
  ?>
    <script>
      document.addEventListener('facetwp-refresh', function() {
        if ('undefined' !== typeof FWP && 'undefined' !== typeof FWP.hooks) {
          FWP.hooks.addFilter('facetwp_map/fit_bounds', function(fit_bounds) {
            return false; // force the custom lat/lng/zoom on every refresh
          });
        }
      });
    </script>
  <?php
}, 100 );