Distance to custom field

Update Sept 2019

This has been added as a predefined custom field in V 2.0.0.67. 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

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.

6 thoughts on “Distance to custom field

  1. Hello, I added the above code to code snippets and it is visible in the custom fields list but when i click to move to my active custom fields for a post type nothing happens… is it possible that the code needs adjustment to be compatible with Geodirectory V2??

    1. Almost certainly. I’ll ask to Stiofan to review it and possibly to update this post for V2.

    2. FYI, the developers added the distance to custom field as a predefined field.

      * Added a new pre-defined field “Distance To” – ADDED

      It’ll be available from V 2.0.0.67 which should come out next week.

      If you want you can download the development version from GitHub and test it: https://github.com/AyeCode/geodirectory

      Let us know,

      Thanks

  2. Good to know users don’t have put the GPS co ordinates manually. More work we put on the user more chances of them not doing it right.

  3. having do have users add latitude and longitude is something that kills this whole system and makes geodirectory kind of worthless for my project

    1. Hi William,

      GPS info is automatically filled in when you move the map or map marker or set the address on the map, you can even hide the GPS info boxes with a setting in the backend so the user never has to see it.

      Stiofan

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.