Wp store locator

<?php

namespace App;

// hide startMarker when searching
add_filter( 'wpsl_js_settings', function($settings) {
  $settings['startMarker'] = 'pink@2x.png'; // Hardcode to make sure styling to hide always works
  return $settings;
}, 10, 1);

// add (and connect) our custom wpsl PHP template
add_filter( 'wpsl_templates', function($templates) {
  $templates[] = array (
    'id'   => 'suiteseven',
    'name' => 'Suite Seven',
    'path' => locate_template('wpsl/locator.php'),
  );
  $templates[] = array (
    'id'   => 'suiteseven_small',
    'name' => 'Suite Seven Klein',
    'path' => locate_template('wpsl/locator-small.php'),
  );
  return $templates;
}, 10, 1);

// remove default wpsl styling
add_action( 'wp_enqueue_scripts', function() {
  wp_dequeue_style( 'wpsl-styles' );
});

// override store-listing (underscore) template
add_filter('wpsl_listing_template', function($template) {
  return template('wpsl/listing');
}, 10, 1);

// override gmaps infowindow (underscore) template
add_filter('wpsl_info_window_template', function($template) {
  return template('wpsl/infowindow');
}, 10, 1);

function getMarkerIcon($store_id) {}
// add custom category (+ acf content) to store meta to show in gmaps
add_filter( 'wpsl_store_meta', function( $store_meta, $store_id ) {
  $terms = get_the_terms( $store_id, 'wpsl_store_category' );
	if ( $terms ) {
		if ( ! is_wp_error( $terms ) ) {
      $icon = get_field('icon', $terms[0]);
      $store_meta['category'] = $terms[0]->name;
      if($icon) {
        $store_meta['categoryIcon'] = $icon;
        $store_meta['categoryMarkerUrl'] = asset_path('images/location/' . $icon . '.png'); // @2x.png possible
        // categoryMarkerUrl is a supported value inside the wpsl plugin
      }
		}
	}
  $store_meta['detail'] = get_field('detail', $store_id) ? true : false;
	return $store_meta;
}, 10, 2 );

add_filter('wpsl_cpt_info_window_meta_fields', function($args, $store_id) {
  $terms = get_the_terms( $store_id, 'wpsl_store_category' );
  // $args = []; // this line will clear all infowindow data so the infowindow won't show
	if ( $terms && ! is_wp_error( $terms ) ) {
    $icon = get_field('icon', $terms[0]);
    if($icon) {
      $args['categoryMarkerUrl'] = asset_path('images/location/' . $icon . '.png'); // @2x.png possible
    }
	}
  return $args;
}, 10, 2);

// marker_props, you know
add_filter('wpsl_marker_props', function($marker_props) {
  $marker_props['scaledSize'] = '40,46';
  // $marker_props['origin'] = '0,0';
  $marker_props['anchor'] = '20,46';
  return $marker_props;
}, 10, 1);

// redirect to /dealers if dealer doesn't have a detailpage
add_action( 'template_redirect', function() {
  if( is_singular( 'wpsl_stores' ) && !get_field('detail', get_the_ID()) ) {
    wp_redirect( home_url( '/dealers/' ), 301 );
    exit();
  }
});

// dont fill the_content with the wpsl stuff
add_filter('wpsl_skip_cpt_template', function() {
  return true;
});

// add defer to some assets
add_filter( 'script_loader_src', function($src) {
  $defer = array(
    'maps.google.com/maps/api',
    'wp-store-locator/js/wpsl-gmap',
  );
  if( !is_admin() && (str_replace($defer, '', $src) != $src) ) {
    echo '<script>defer_wpsl = window.defer_wpsl || []; defer_wpsl.push("' . $src . '");</script>'; // pay attention here o.0
    $src = false; // this will stop the script from loading (as we'll load it manually from javascript)
  }
  return $src;
}, 1000 );

// remove permalink for wpsl category taxonomy
add_filter('wpsl_store_category_args', function($args) {
  $args['rewrite'] = false;
  $args['public'] = false;
  return $args;
}, 10);


// add_action('wp_footer', function() {
//   wp_dequeue_script( 'wpsl-gmap' );
// });
Last Updated:
Contributors: Niek Vlam, Suite Seven