• Resolved mwhiteley16

    (@mwhiteley16)


    Is there a built-in way to create a city-based button. Like if I need to search Austin, instead of the user typing it into the search input there can be buttons with key cities the user could click and it would search based on that city?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there,

    Well, there is not a built-in solution for that but we can do the trick using a bit of javascript. Place this code snippet at the end of your active theme’s functions.php file:

    add_action( 'wp_footer', function () { ?>
      <script type='text/javascript'>
        // pure jquery snippet for loading the search from GET parameter in the search field and clicking the button
        jQuery( window ).load(function() {
          let searchParams = new URLSearchParams(window.location.search);
    
          if (searchParams.has('location')) {
            let location = searchParams.get('location');
            jQuery('#wpsl-search-input').val(location);	
          } else {
            console.log('location not received');
          }
      });
    </script>
    <?php } );

    Then the site is ready to receive the “location” parameter in the url, so if for instance your wp store locator is showing at https://mydomain.com/stores, and you want to create predefined buttoms with the cities “Omaha” and “Phoenix”, you can insert buttons that link to these urls, and they will work:

    <a href="https://mydomain.com/stores?location=Omaha">Omaha</a>
    <a href="https://mydomain.com/stores?location=Phoenix">Phoenix</a>

    I hope that helps.

    Regards ??

    • This reply was modified 2 years, 6 months ago by farroyo.
    • This reply was modified 2 years, 6 months ago by farroyo.
    Thread Starter mwhiteley16

    (@mwhiteley16)

    Wonderful – thank you for the guidance. This should work perfectly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Button to search for specific city’ is closed to new replies.