Advertisement
Guest User

WordPress: Add Geo My WP to Postie

a guest
Jun 18th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. /**********************************************************
  3.  * This function extends Geo My WP to work with posts     *
  4.  * submitted via the Postie plugin. Those posts bypass    *
  5.  * the addition of the needed meta fields for geolocation *
  6.  * searches. This adds them back in.                      *
  7.  **********************************************************/
  8.  
  9. function add_post_location($post) {
  10.  
  11.     while(!is_plugin_active("geo-my-wp/geo-my-wp.php")) {
  12.         usleep(100);
  13.     }
  14.  
  15.     // Initialize variables.
  16.     $id = $post->ID;
  17.     $author_id = $post->post_author;
  18.     $street_address = xprofile_get_field_data("Street Address", $author_id); // Assumes an xprofile field of Street Address
  19.     $city = xprofile_get_field_data("City", $author_id); // Assumes an xprofile field of City
  20.     $state = xprofile_get_field_data("State", $author_id); // Assumes an xprofile field of State
  21.     $zipcode = xprofile_get_field_data("ZIP Code", $author_id); // Assumes an xprofile field of Zip Code
  22.  
  23.     // Build and set the full address to search.
  24.     $address = '';
  25.     if($street_address != '')
  26.         $address .= $street_address + ", "; // Only add the street address if it has been set. It's an optional field in my setup.
  27.     $address .= $city . ", " . $state . " " . $zipcode;
  28.  
  29.     // Obtain geocoding data from Google via Geo My WP function.
  30.     $coords = gmwConvertToCoords($address);
  31.  
  32.     // Set the post coordinates.
  33.     add_post_meta($id, "_wppl_lat", $coords['lat']);
  34.     add_post_meta($id, "_wppl_long", $coords['long']);
  35.  
  36.     // Set the individual fields for street address, city, state, and ZIP code.
  37.     add_post_meta($id, "_wppl_street", $coords['street']);
  38.     add_post_meta($id, "_wppl_city", $coords['city']);
  39.     add_post_meta($id, "_wppl_state", $coords['state_short']);
  40.     add_post_meta($id, "_wppl_state_long", $coords['state_long']);
  41.     add_post_meta($id, "_wppl_zipcode", $coords['zipcode']);
  42.     add_post_meta($id, "_wppl_country", $coords['country_short']);
  43.     add_post_meta($id, "_wppl_country_long", $coords['country_long']);
  44.  
  45.     // Heck, set the formatted address while we're at it.
  46.     add_post_meta($id, "_wppl_formatted_address", $coords['formatted_address']);
  47.  
  48.     // Set the map icon to the default.
  49.     add_post_meta($id, "_wppl_map_icon", "_default.png");
  50.  
  51.     // Add the email address.
  52.     add_post_meta($id, "_wppl_email", xprofile_get_field_data("Email", $author_id));
  53.  
  54.     // Add the phone numbers. Only uses the ones that have been filled out.
  55.     $home_phone = xprofile_get_field_data("Home Phone", $author_id);
  56.     $cell_phone = xprofile_get_field_data("Cell Phone", $author_id);
  57.     $phone = '';
  58.  
  59.     if($home_phone != '' && $cell_phone == '')
  60.         $phone = $home_phone;
  61.     if($home_phone == '' && $cell_phone != '')
  62.         $phone = $cell_phone;
  63.     if($home_phone != '' && $cell_phone != '')
  64.         $phone = $home_phone + " / " + $cell_phone;
  65.     add_post_meta($id, "_wppl_phone", $phone);
  66.     return $post;
  67. }
  68.  
  69. add_filter("postie_post", "add_post_location");
  70.  
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement