Distance to custom field

Update Sept 2019

This has been added as a predefined custom field in V The snippet below will only work if using GeoDirectory V1.

The Problem

Today a member had a bit of an unusual request, they are using GeoDirectory to run a local festival and they are listing things like accommodation for it. The request was to show the distance in each listing to the main event so users have a clear understanding of how far it will be to the event from each listing. I do something very similar on one of my own sites that is for one location, i list the distance to the airport and the distance to the main town, i have until now just had users enter this distance manually, this is often wrong and i have to correct it.

The Solution

This seemed like the perfect time to create a new custom field, lets call it “Distance to”. The code is below but there are two ways to use this field:
#1 We could just add the field and let the user enter GPS coordinates, it might be for example a “closest airport” field, the user could then enter the GPS info for the closest airport to them.
#2 In this users case he just needs to show the distance to one place and without the user really needing to even know about the field, so when he is adding the custom field he would set the field as a “admin only” field so it is not shown to the user on the frontend and they can’t change it, and then set the “default value” to the GPS coordinates needed and all listings will then show the info needed.

The Code

Unlock the Power of GeoDirectory!

Start your Online Business Now

Join Today!

Stay Updated!

Get the latest news, tips, and exclusive content directly in your inbox.

Published by Stiofan O'Connor

Stiofan O'Connor is the co-founder and CEO of AyeCode LTD. With his business partner Paolo, they are the makers of the GeoDirectory, UsersWP and Invoicing plugins for WordPress. He is a Full Stack developer specialized in WordPress development. Stiofan started building websites as a hobby in the early 2000s with PHP at first. He then moved to the CodeIgniter Web Framework for a while before discovering WordPress and ever since he never looked back. Today his WordPress themes and plugins are used with success by +100.000 active websites.