Events Manager
[resolved] add simple placeholder for zip code distance (13 posts)

  1. melon503
    Posted 3 years ago #

    I have managed to alter the events-manager queries so that I can search for a zip code within a certain radius, and that information is now being output in the query as an alias called HowFar. The problem I'm having is figuring out how to display that output on the page. From what I gather, I need to create a placeholder, but I have not been able to figure out how to do that. Seems like it would be the easiest part.

    Any suggestions are much appreciated.


  2. agelonwl
    Posted 3 years ago #

    a working sample snippet

    function my_em_location_longitude_placeholders($replacement, $EM_Location, $result){
        global $wp_query, $wp_rewrite;
        switch( $result ){
    		$replacement = $EM_Location->location_longitude;
        return $replacement;

    For more info:

  3. melon503
    Posted 3 years ago #

    Very helpful agelonwl - thanks a ton for taking the time to post this.

  4. JanetB
    Posted 2 years ago #

    Would you share this with us, melon503?
    I would like to add the same option to my events manager.

    How did you alter the queries so that you can search for a zip code within a certain radius??

  5. chrishylton2012
    Posted 2 years ago #

    Hey melon503, I know this is from 2 months ago, but this is exactly what I need to implement in my site. Would you mind sharing how you implemented the zipcode radius search?

  6. melon503
    Posted 2 years ago #

    Hey All,

    I'm not claiming that this is a clean way to do this, but basically I used the existing postcode field and then added the following to line 231 of em-events.php :

    // this filter adds the logic to the SQL query to take the postcode and compare it to the lat / lng of the location.
    add_filter( 'em_events_build_sql_conditions', 'my_em_scope_conditions',1,2);
    function my_em_scope_conditions($conditions, $args){
    	if( !empty($args['postcode']) ){
    	$base_url = "http://maps.google.com/maps/geo?output=xml&key=[your google key here]";
        $request_url = $base_url . "&q=" . urlencode($args['postcode']);
        $xml = simplexml_load_file($request_url);
        $status = $xml->Response->Status->code;
        if (strcmp($status, "200") == 0) {
          // Successful geocode
          $geocode_pending = false;
          $coordinates = $xml->Response->Placemark->Point->coordinates;
          $coordinatesSplit = split(",", $coordinates);
          // Format: Longitude, Latitude, Altitude
          $lat = $coordinatesSplit[1];
          $lng = $coordinatesSplit[0];
    	$conditions['scope'] = " (3956 * 2 * ASIN(SQRT(POWER(SIN(('".$lat."' - abs(location_latitude))*pi()/180/2),2) + COS('".$lat."' * pi()/180) * COS(abs(location_latitude) * pi()/180) * POWER(SIN(('".$lng."' - location_longitude) * pi()/180/2),2))) < '".$radius."')";	
    	return $conditions;

    [Moderator Note: Please post code or markup snippets between backticks or use the code button.]

  7. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 2 years ago #

    That's along the lines of how I imagined that going (maybe better?)

    Thanks for contributing, I'll test that out and integrate!

  8. alan604
    Posted 2 years ago #

    I'm a complete beginner at PHP and am stuck.

    I've loaded em-events.php into a php editor, found line 231, added the code above (with a change to match Google Maps API 3 as below) immediately after it, but no postcode box shows on the Events page where all the search parameters are.

    My revised API line is:
    $base_url = "http://maps.googleapis.com/maps/api/geocode/xml?address";

    Could someone please let me know what I'm doing wrong?

    I'm expecting to see an input box for postcode and radius - is that right please?

    It would also help me if someone who has it working could post the actual line 231 referenced, just in case the editor I've used is doing something different.

    Thanks in advance!

  9. adzay
    Posted 2 years ago #

    Hi Marcus,

    I want to implement this into the Events-Search form.
    Which document in Events manager website will tell me how to use filters for searching?

  10. adzay
    Posted 2 years ago #

    Never mind I found it :)


    Alan604 ..I think you make it into a scope ( the link above) and then add it to the EM_EVENTS::output.

    Then you need to create form fields.. one with the name "postcode". I haven't figured out where you put in the radius yet though.

    ( It may not work though. I'm not good at php I only learn through trial and error lol)

    Marcus, was you able to try this out. Are there any caveats that I should worry about. I'm at work at the moment but eager to try it out when I get home.

  11. adzay
    Posted 2 years ago #

    I just had a thought about this. Does the original code that Melon made mean that whenever you enter in your postcode, it will ALWAYS give you results within a predefined radius?

  12. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 2 years ago #

    We've been looking into this, and it turns out that the code above doesn't work, because it uses a defunct Google API. The new API requires payments, so we're looking into a JS alternative so there's no request limits. Also, $input_radius isn't defined (it = miles)

    That said, once you have the correct long/lat the second part of the snippet should work.

    I think the code was altered from here - http://jebaird.com/blog/calculating-distance-miles-latitude-and-longitude

    Chances are using geonames would also work. The tricky part is actually finding the lng/lat values of the location the user is searching

  13. JanetB
    Posted 2 years ago #

    Hired a php-programmer who made a postcode-distance check for me in Events-Manager. This doesn't work anymore and now I understand why: he used the Google API.

    Anyway, all the adjustments generate a lot of work with every update. So I hope the zipcode check will be in Events-manager soon.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Events Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic