Forum Replies Created
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
Alperen Ozkan
ParticipantWhere can I find $meta_box_id ?
Alperen Ozkan
ParticipantHi Long,
I am trying to get not only location data but also place details. I have fixed my code like below, but I really appreciate it if you can help me to save this data to a custom table which I have already created with metabox custom table extension.<?php add_action( 'save_post', 'google_places_data', 10, 4 ); function google_places_data( $post = null ) { global $post; if($post->post_type == 'hamburg'){ if ( ! $post = get_post( $post ) ) return; if ( ! $place_id = get_post_meta( $post->ID, 'place_id', true ) ) return; // No place ID configured for this post if ( $data = get_post_meta( $post->ID, 'place_data', true ) ) { if ( $data->timeout <= current_time( 'timestamp' ) ) $data = null; // Void the cache if it's old } if ( ! $data ) { $args = http_build_query( array( 'key' => 'MY API KEY', // API key 'place_id' => $place_id ) ); $http = wp_remote_get( "https://maps.googleapis.com/maps/api/place/details/json?$args" ); if ( $body = wp_remote_retrieve_body( $http ) ) { if ( $data =@ json_decode( $body ) ) $data->timeout = current_time( 'timestamp' ); // Cache data for 1 hour $address_components_length = count($data->result->address_components); $bezirk; $city; $state; $post_code; foreach ($data->result->address_components as $component_data){ $type = $component_data->types[0]; if ($type == 'sublocality_level_1') { $bezirk = $component_data->long_name; } else if ($type == 'locality') { $city = $component_data->long_name; } else if ($type == 'administrative_area_level_1') { $state = $component_data->long_name; } else if ($type == 'postal_code') { $post_code = $component_data->long_name; } } $name = $data->result->name; $formatted_address = $data->result->formatted_address; $formatted_phone_number = $data->result->formatted_phone_number; $rating = $data->result->rating; $total_rating = $data->result->user_ratings_total; $google_maps_url = $data->result->url; $website = $data->result->website; update_post_meta( $post->ID, 'name', $name ); update_post_meta( $post->ID, 'formatted_address', $formatted_address ); update_post_meta( $post->ID, 'formatted_phone_number', $formatted_phone_number ); update_post_meta( $post->ID, 'bezirk', $bezirk ); update_post_meta( $post->ID, 'city', $city ); update_post_meta( $post->ID, 'state', $state ); update_post_meta( $post->ID, 'post_code', $post_code ); update_post_meta( $post->ID, 'rating', $rating ); update_post_meta( $post->ID, 'total_rating', $total_rating ); update_post_meta( $post->ID, 'google_maps_url', $google_maps_url ); update_post_meta( $post->ID, 'website', $website ); } if ( $data ) { update_post_meta( $post->ID, 'place_data', $data ); } else { echo 'api error'; } } return $data; } } -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)