Locations API

This article explains how to customize: locations that WPJB displays in the frontend and search parameters used for geocoding locations (this will allow you to fine tune locations on maps if needed).

Customizing locations display

In order to change how ALL locations in WPJB are displayed you can take advantage of wpjb_location_display filter, the code below will change location format to “{city}, {country}” for example “Paris, France”.

add_filter("wpjb_location_display", "my_location_display", 10, 2);
function my_location_display($addr, $object) {
    if($object instanceof Wpjb_Model_Job) {
        $country_code = $object->job_country;
        $state = $object->job_state;
        $zip_code = $object->job_zip_code;
        $city = $object->job_city;
    } elseif($object instanceof Wpjb_Model_Resume) {
        $country_code = $object->candidate_country;
        $state = $object->candidate_state;
        $zip_code = $object->candidate_zip_code;
        $city = $object->candidate_location;
    } elseif($object instanceof Wpjb_Model_Company) {
        $country_code = $object->company_country;
        $state = $object->company_state;
        $zip_code = $object->company_zip_code;
        $city = $object->company_city;
    }

    // maybe some meta here, if for example you have a custom field
    // with name address you can uncomment line below
    // $address = $object->meta->address->value();

    $country = Wpjb_List_Country::getByCode($country_code);

    /* @var $country Array ("code"=>int, "iso2"=>string, "iso3"=>string, "name"=>string") */

    // select how location should be displayed

    // city only
    // return $city

    // city state
    // retrun "$city, $state";

    // city country
    return $city.", ".$country["name"]; 
}

The code might look quite long, however most important is the code at the end (after “select how location should be displayed”) that formats location and returns it. Rest of the code is more of a general template that allows to pick any information about locations that WPJB has.

Modifying Geocoding Search

Geocoding is a proccess of turning location string for example “Empire State Building, 5th Avenue, New York, NY” into latitude and longitude data. WPJB uses this data to display locations on Google Maps, by modifying how and which location information will be sent to geocoding service you can match the locations more precisely.

Lets assume that you created “address” field in the Add Job Form using Custom Fields panel. In order to pass the “address” to geocoding service you could use wpjb_geolocate filter, see example below:

add_filter("wpjb_geolocate", "my_wpjb_geolocate", 10, 2);
function my_wpjb_geolocate($addr, $object) {
    if($object instanceof Wpjb_Model_Job) {
        // add address custom field value to the beginning of 
        // address that will be used to geocoding location
        array_unshift($addr, $object->meta->address->value());
    }
    
    return $addr;
}

Now the geocoding service will lookup addresses in following format “{address}, {city}, {zip_code}, {state}, {country}”.

  1. nesbittangeline@googlemail.com
    Reply

    Please, can you tell us the file to use in order to do those modifications.
    Thanks,
    Angeline

  2. Can i use this function to create a search? In other words if the user lives in the state of Michigan can i parse that data and create a location search that narrows the job list to the state of Michigan as if the user had selected the state from the sidebar?

    Or is this function only use to pinpoint a location on Google Maps?

    • Greg
      Reply

      Not really, the Locations API is mainly for controlling how the locations will be displayed in the front-end and how they should be geo located.

  3. Stephan
    Reply

    (presale)
    Is possible geomywp plugin work fine with wpjobboard ?
    or is possible wpjobboard team make this plugin work ?

    • Greg
      Reply

      @Stephan, currently (as of version 4.1.4) not, however WPJB already has geolocation data saved in DB and creating jobs map is just a matter of using Google Maps API.

  4. Jan
    Reply

    How do you set up Google maps to work with the job listings?

    Thank you!

    -Jan

    • Greg
      Reply

      It is best to start with configuring Google APIs https://wpjobboard.net/kb/google-apis/, then when publishing or updating a job the Google Map geo data should be automatically calculated and if matching location is found the map will be shown without any other action on your part required.

  5. Hi,

    The map shows up on my development website and not on the real website. It is frustrating as I do not see any differences as far as how I have entered information. Any ideas?

    -Jan

  6. Hi, i’ve used the above code. It worked. But i wanted to add some html (so i can design) inside the result of the address. How can i do it? i tried the below code, it directly shows up the html (look at the last line)—

    add_filter(“wpjb_location_display”, “my_location_display”, 10, 2);
    function my_location_display($addr, $job) {
    if($job instanceof Wpjb_Model_Job) {
    $country_code = $job->job_country;
    $state = $job->job_state;
    $zip_code = $job->job_zip_code;
    $city = $job->job_city;
    }

    $extra_cities = $job->meta->extra_cities->value();

    $country = Wpjb_List_Country::getByCode($country_code);

    return $city.”””.$extra_cities.””;
    }

    • Greg
      Reply

      Hi, i am afraid it’s not really possible to have HTML there, the HTML in your location will be escaped before displaying the value in template (in wpjobboard/templates/job-board/index-item.php and job.php).

Leave a Reply

Your email address will not be published.