Support

Account

Home Forums General Issues update_field not working unless die() in function Reply To: update_field not working unless die() in function

  • I have changed the code around, but here is the entire plugin, again it will ONLY save the update_field when I have the die(); that is show in this.

    function nbpe_post_unpublished( $new_status, $old_status, $post ) {
        if ($new_status != 'trash'){ // If item is being deleted, don't do anything.
    		if ($post->post_type == 'project'){ // Make sure this is a "project" post, not a regular post.
    			if ( $old_status != 'publish'  &&  $new_status == 'publish') { // A function to perform actions when a post status changes from any non-published to published status.
    					do_action( 'save_post', $post->ID, $post, true );
    					nbpe_build_address_string($post);
    			}
    		}
    	}
    }
    add_action( 'transition_post_status', 'nbpe_post_unpublished', 10, 3 );
    
    function nbpe_build_address_string($post){
    	$post_id = $post->ID;
    	$street_number = get_field('street_number');
    	$street_direction = get_field('street_direction');
    	if ($street_direction == 'NONE'){
    		$street_direction = '';
    	}
    	else{
    		$street_direction = $street_direction.'+';
    	}
    	$street_name = str_replace(' ', '+',get_field('street_name'));
    	$street_suffix = get_field('street_suffix');
    	if ($street_suffix == 'NONE'){
    		$street_suffix ='';
    	}
    	else{
    		$street_suffix = $street_suffix.'+';
    	}
    	$city = get_field('city');
    	$state = get_field('state');
    	$zip_code = get_field('zip_code');
    	$address_space = '+';
    	$address_string = $street_number.$address_space.$street_direction.$street_name.$address_space.$street_suffix.$zip_code;
    	geoencode($address_string, $post_id);
    }
    
    function geoencode($address_string, $post_id){
    	$url_string = 'https://maps.googleapis.com/maps/api/geocode/xml?address='.$address_string;
    	$xmlStr = file_get_contents($url_string); //Read the XML returned by Google maps API into a string.
    	$xmlObj = simplexml_load_string($xmlStr); //Interprets a string of XML into an object.
    	$arrXml = objectsIntoArray($xmlObj); // Converts to an array.
    	
    	$status = $arrXml['status'];
    			
    		$latitude = $arrXml["result"]["geometry"]["location"]["lat"];
    		$longitude = $arrXml["result"]["geometry"]["location"]["lng"];
    	
    		echo 'Status: '.$status.'<br />';
    		echo 'Lat: '.$latitude.'<br />';
    		echo 'Lng: '.$longitude.'<br />';
    	
    		update_field('latitude', $latitude, $post_id);
    		update_field('longitude', $longitude, $post_id);
    	
    	die('<a href=edit.php?post_type=project><h1>Continue</h1></a>');
    }
    
    function objectsIntoArray($arrObjData, $arrSkipIndices = array())
    {
        $arrData = array();
        
        // if input is object, convert into array
        if (is_object($arrObjData)) {
            $arrObjData = get_object_vars($arrObjData);
        }
        
        if (is_array($arrObjData)) {
            foreach ($arrObjData as $index => $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }