Working with Geolocation in
WordPress
@brianhogg
http://slideshare.net/bhogg
Overview
@brianhogg WordCamp Buffalo Sep 2013 2
 Why Geolocation
 Capturing the Data
 Using the Data
 Getting Visitor ...
About Brian Hogg
@brianhogg WordCamp Buffalo Sep 2013 3
 Software Engineering & Management
 Started and sold DrawBINGO.c...
Why Geolocation
@brianhogg WordCamp Buffalo Sep 2013 4
 Events blog
 Recommendations
 Mapping
 Store locations
 … any...
5@brianhogg WordCamp Buffalo Sep 2013
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 6
 “Official” Geolocation plugin
http://wordpress.org/extend/plug...
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 7
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 8
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 9
Inserts a latitude and longitude for each post in the
post meta ...
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 10
 Import from CSV
Capturing the Data
@brianhogg WordCamp Buffalo Sep 2013 11
 Translating address string to lat/lon (geocode)
http://maps.g...
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 12
 Simple front-end pop-up
 Using WP_Query (meta_query)
 Hooks to ...
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 13
 Simple front-end pop-up
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 14
 Database query
 Get location of user
 Restrict posts using a “b...
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 15
 Using WP_Query (meta_query)
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 16
 Bounding box query
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 17
 Using WP_Query
@brianhogg WordCamp Buffalo Sep 2013 18
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 19
 Using WP_Query
@brianhogg WordCamp Buffalo Sep 2013 20
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 21
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN
wp_...
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 22
 WP_Query method very inefficient especially with
larger amounts o...
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 23
 Create a new indexed table in the database
 Create a WP hook to ...
@brianhogg WordCamp Buffalo Sep 2013 24
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 25
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 26
 posts_clauses filter
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 27
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 28
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 29
 Custom feeds
 Page templates
 Caching?
Using the Data
@brianhogg WordCamp Buffalo Sep 2013 30
 Create map markers from
one or more JSON based
feeds
Getting Visitor Location
@brianhogg WordCamp Buffalo Sep 2013 31
 IP address (approximate)
 GPS
 Google Maps API
Getting Visitor Location
@brianhogg WordCamp Buffalo Sep 2013 32
 IP address (approximate)
 MaxMind – www.maxmind.com
 ...
@brianhogg WordCamp Buffalo Sep 2013 33
Resources / Contact
brian@bhconsulting.ca
@brianhogg
Upcoming SlideShare
Loading in …5
×

Working with Geolocation in Wordpress

3,844 views

Published on

Published in: Technology, Business
  • Be the first to comment

Working with Geolocation in Wordpress

  1. 1. Working with Geolocation in WordPress @brianhogg http://slideshare.net/bhogg
  2. 2. Overview @brianhogg WordCamp Buffalo Sep 2013 2  Why Geolocation  Capturing the Data  Using the Data  Getting Visitor Location
  3. 3. About Brian Hogg @brianhogg WordCamp Buffalo Sep 2013 3  Software Engineering & Management  Started and sold DrawBINGO.com  Lived in Montréal, England, now Cambridge (Ontario)  PHP/JS since 2003
  4. 4. Why Geolocation @brianhogg WordCamp Buffalo Sep 2013 4  Events blog  Recommendations  Mapping  Store locations  … anything with content + latitude/longitude
  5. 5. 5@brianhogg WordCamp Buffalo Sep 2013
  6. 6. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 6  “Official” Geolocation plugin http://wordpress.org/extend/plugins/geolocation/  WP-Geo http://wordpress.org/extend/plugins/wp-geo/ (for custom post types)  WordPress native apps  … other plugins / themes Inserting a latitude and longitude for posts.
  7. 7. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 7
  8. 8. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 8
  9. 9. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 9 Inserts a latitude and longitude for each post in the post meta data. http://codex.wordpress.org/Geodata
  10. 10. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 10  Import from CSV
  11. 11. Capturing the Data @brianhogg WordCamp Buffalo Sep 2013 11  Translating address string to lat/lon (geocode) http://maps.googleapis.com/maps/api/geocode/xml? address=Hamilton+ON&sensor=false <geometry> <location> <lat>43.2500208</lat> <lng>-79.8660914</lng> </location> <location_type>APPROXIMATE</location_type> </geometry>
  12. 12. Using the Data @brianhogg WordCamp Buffalo Sep 2013 12  Simple front-end pop-up  Using WP_Query (meta_query)  Hooks to store in separate, indexed table  Custom feeds  …
  13. 13. Using the Data @brianhogg WordCamp Buffalo Sep 2013 13  Simple front-end pop-up
  14. 14. Using the Data @brianhogg WordCamp Buffalo Sep 2013 14  Database query  Get location of user  Restrict posts using a “bounding box”  Ordering by distance from a lat/lon
  15. 15. Using the Data @brianhogg WordCamp Buffalo Sep 2013 15  Using WP_Query (meta_query)
  16. 16. Using the Data @brianhogg WordCamp Buffalo Sep 2013 16  Bounding box query
  17. 17. Using the Data @brianhogg WordCamp Buffalo Sep 2013 17  Using WP_Query
  18. 18. @brianhogg WordCamp Buffalo Sep 2013 18
  19. 19. Using the Data @brianhogg WordCamp Buffalo Sep 2013 19  Using WP_Query
  20. 20. @brianhogg WordCamp Buffalo Sep 2013 20
  21. 21. Using the Data @brianhogg WordCamp Buffalo Sep 2013 21 SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) LEFT JOIN wp_geo geo ON wp_posts.ID = geo.post_id WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'geo_latitude' AND CAST(wp_postmeta.meta_value AS DECIMAL) BETWEEN '42' AND '43') AND (mt1.meta_key = 'geo_longitude' AND CAST(mt1.meta_value AS DECIMAL) BETWEEN '-79.9999' AND '-78') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 15  Sample WP_Query SQL (bounding box)
  22. 22. Using the Data @brianhogg WordCamp Buffalo Sep 2013 22  WP_Query method very inefficient especially with larger amounts of data  Separate table can be used with an index, numeric data format
  23. 23. Using the Data @brianhogg WordCamp Buffalo Sep 2013 23  Create a new indexed table in the database  Create a WP hook to store in the new table CREATE TABLE IF NOT EXISTS `wp_geo` ( `post_id` bigint(20) unsigned NOT NULL, `lat` float NOT NULL, `lon` float NOT NULL, PRIMARY KEY (`post_id`), KEY `latlon` (`lat`,`lon`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  24. 24. @brianhogg WordCamp Buffalo Sep 2013 24
  25. 25. Using the Data @brianhogg WordCamp Buffalo Sep 2013 25
  26. 26. Using the Data @brianhogg WordCamp Buffalo Sep 2013 26  posts_clauses filter
  27. 27. Using the Data @brianhogg WordCamp Buffalo Sep 2013 27
  28. 28. Using the Data @brianhogg WordCamp Buffalo Sep 2013 28
  29. 29. Using the Data @brianhogg WordCamp Buffalo Sep 2013 29  Custom feeds  Page templates  Caching?
  30. 30. Using the Data @brianhogg WordCamp Buffalo Sep 2013 30  Create map markers from one or more JSON based feeds
  31. 31. Getting Visitor Location @brianhogg WordCamp Buffalo Sep 2013 31  IP address (approximate)  GPS  Google Maps API
  32. 32. Getting Visitor Location @brianhogg WordCamp Buffalo Sep 2013 32  IP address (approximate)  MaxMind – www.maxmind.com  HostIP - http://www.hostip.info/use.html  http://ipinfodb.com/ip_location_api.php  Javascript request (navigator.geolocation)
  33. 33. @brianhogg WordCamp Buffalo Sep 2013 33 Resources / Contact brian@bhconsulting.ca @brianhogg

×