Redirect to the Store Website URL via the Search Widget

This documentation explains how to implement a redirection to the closest store’s website URL using the Agile Store Locator plugin in WordPress. The redirection occurs based on the address entered in a search widget. This functionality uses the template_redirect action hook in WordPress.

Prerequisites #

  1. Agile Store Locator plugin installed and configured.
  2. A valid Google Maps Server API key is set in the plugin’s configurations.
  3. Agile Store Locator Search Widget setup.

Code Implementation #

Add the following code to your theme’s functions.php file or a custom plugin to enable redirection:

add_action('template_redirect', function() {

    // Check if 'sl-addr' is set and either 'lat' or 'lng' is not set in the URL parameters
    if (isset($_GET['sl-addr']) && (!isset($_GET['lat']) || !isset($_GET['lng']))) {
        // Retrieve the server API key from the configurations
        $api_key = \AgileStoreLocator\Helper::get_configs('server_key');

        // Get coordinates (latitude and longitude) based on the address
        $coordinates = \AgileStoreLocator\Helper::getCoordinates(null, $_GET['sl-addr'], null, null, 'UK', $api_key);

        // Extract latitude and longitude from the coordinates
        $lat = $coordinates['lat'];
        $lng = $coordinates['lng'];

    } elseif (isset($_GET['sl-addr'])) {
        // If 'sl-addr' is set and 'lat' and 'lng' are also set, use them directly
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];

    // Check if 'sl-addr' is set and lat/lng are available
    if (isset($_GET['sl-addr']) && isset($lat) && isset($lng)) {

        // Define the radius in miles for searching the closest store
        $miles = 100;

        // Get the closest store within the specified radius that matches the criteria
        $store = \AgileStoreLocator\Helper::get_closest_store(floatval($lat), floatval($lng), $miles, [
            //'categories' => 2, // Category filter for the store
            'website' => '*',  // Filter to ensure the store has a website

        // If a store is found, redirect to its website
        if ($store) {
            exit; // Ensure the script stops executing after the redirect

The above code automatically redirects the user to the closest store’s website based on the address they enter into a search widget. This functionality is particularly useful for enhancing the user experience by providing direct access to the nearest store’s website.

Detailed Steps #

  1. Checking URL Parameters:
    • The script first checks if the sl-addr parameter is set in the URL.
    • If the lat and lng parameters are not set, and the script retrieves the Google Maps API key from the plugin’s configurations.
    • It then calls the getCoordinates function to get the latitude and longitude based on the provided address.
  2. Using Provided Coordinates:
    • If the lat and lng parameters are already set in the URL, these values are used directly.
  3. Finding the Closest Store:
    • The script checks again to ensure sl-addr, lat, and lng are available.
    • It defines a search radius of 100 miles and calls the get_closest_store function to find the nearest store that matches the specified criteria (category and website availability).
  4. Redirecting to the Store Website:
    • If a store is found, the script redirects the user to the store’s website using wp_redirect.
    • The exit; statement ensures that no further code is executed after the redirection.