Support

Account

Home Forums Backend Issues (wp-admin) Get request while save post

Solved

Get request while save post

  • Hey Guys,
    first never ever seen such great plugin like yours! thumbup.
    Second, I hope this is the right section otherwise sorry and move 🙂

    I’m trying to do a get request while pre_save_post. Sometimes it works, mosttime not.

    Some src:

    function my_pre_save_post( $post_id )
    {
    	
    	if($post_id !== 'new_post')
    	{ 
    		return $post_id;	
    	}
    	
    	$title = $_POST['fields']['field_5984a6941d5e9'];
    	
    	$post = array(
    		'ID'	=>	$title,
    		'post_status' => 'publish',
    		'post_type' => 'kunden_type',
    		'post_title'	=> $title,
    	);	
    	
    	$post_id = wp_insert_post($post); 
    
    	$data = sanitize_title($_POST['fields']['field_5984a79b1d5ee']) . "+" . $_POST['fields']['field_598e3e3921695'] . "+" . $_POST['fields']['field_5984b0661d5ef'];
    
    	$baseurl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $data;
    
    	$response = wp_remote_get($baseurl, array('timeout' => 5));
    	
    	if($response['response']['code'] == 200)
    	{
    		$api_response = json_decode(wp_remote_retrieve_body($response), true);
    
    		update_field('field_59905f6f6027d', $api_response['results'][0]['geometry']['location']['lat'], $post_id);
    		update_field('field_599060066027e', $api_response['results'][0]['geometry']['location']['lng'], $post_id);
    	}
    	/*
    	$ch = curl_init();
       	curl_setopt($ch, CURLOPT_URL, $baseurl);
       	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       	$geoloc = json_decode(curl_exec($ch), true);
    	*/
    	
    	return $post_id;
    }

    I also tryed curl but its the same result.
    If it fails(?) no post created
    If it succeded a post created and lat /lng are placed

    Is it cause some async stuff?

  • Never mind, got it.
    Just needed to add acf/save_post hook too 🙂

  • Any chance of posting the complete snippet? I’m not skilled at anything apart from making mistakes…

    This looks to me like it might do what I’m hoping… At the moment I have front-end forms that can create and submit custom posts. I’m using formidable forms for the form part.

    At the moment, when a post is created this way I am forced to have it submit as a draft post, because the google map field will be empty.

    When users input their address in the form, isn’t geocoded – no lat/lng – so the map doesn’t exist until I open a post and trigger the map, then save it.

    This looks like it might find the lat/lng when the post is created. Is that right?

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get request while save post’ is closed to new replies.