0

I am trying to use Google Places API with WordPress. I am trying to create a function where I get the API data and store it as custom fields.

I found a similar answer, but it only deals with cache.

This answer helped but not in detail.

I want to use wp_remote_get() to retrieve JSON data.

My goal is to access any the custom fields data using get_post_meta() in single.php

4
  • so what is the question? how to use the google api itself is off-topic here Sep 12 '17 at 14:33
  • what is the problem with storing key,pair as a custom field? key is the meta key, and pair is the value for example Sep 12 '17 at 15:01
  • the function in my question is not working I don't know what I am doing wrong? After adding the place_id its suppose to auto populate or update the other custom fields Sep 12 '17 at 15:16
  • you need ot debug what is it that you are getting from the api Sep 12 '17 at 16:13
0

I found out how I can store JSON data into a custom field. I completely forgot to use add_action.

Here is my answer below I added to functions.php.

You have to add custom field place_id and its value then click 'publish or 'update' for the other fields to populate.

    function google_places_data( $post = null ) {
    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' => 'AIzaSyCarm54WzOXFhXqmEU3rkUorDoa8b3Nzog', // API key
                'placeid' => $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' ) + HOUR_IN_SECONDS; // Cache data for 1 hour

            $place_address = $data->result->formatted_address; 
            $place_lat = $data->result->geometry->location->lat;
            $place_long = $data->result->geometry->location->lng; 
            $place_phone = '000-000-010';

            update_post_meta( $post->ID, 'place_address', $place_address );
            update_post_meta( $post->ID, 'place_lat', $place_lat );
            update_post_meta( $post->ID, 'place_long', $place_long );
            update_post_meta( $post->ID, 'place_phone', $place_phone );

        }

        if ( $data ) {
            update_post_meta( $post->ID, 'place_data', $data );

        } else {
            echo 'api error';
        }

    }

    return $data;
}
add_action( 'save_post', 'google_places_data', 10, 4 );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.