Forum Replies Created

Viewing 11 replies - 1 through 11 (of 11 total)
  • I am too! it is on a network site setup.
    I have deleted the plugin and am still getting the 500.
    WTH?

    Thread Starter GHMedia

    (@ghmedia)

    Hi Dave, here is the code, it starts with the transition_post_status. Build an address string, geoencodes it and (attempts) to save the lat and long back to the post. If I have the die() it does save it, if I don’t have the die() it does a screen refresh, but nothing is in the lat and long fields.

    add_action('transition_post_status','nbpe_publish_project',10,3);
    
    function nbpe_publish_project($new_status,$old_status,$post){
    	if ($post->post_type == 'project' && $new_status != 'trash' && $old_status !='new'){
    		do_action( 'save_post', $post->ID, $post, true );
    		if (get_field('latitude') =='' || get_field('longitude') == ''){
    			nbpe_geoencode($post,$post->ID);
    		}
    	}
     }
    
    // Geoencoding
    function nbpe_geoencode($post,$postID){
    	$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;
    	$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'];
    
    	if ($status == 'OK'){ //Geoencode was successful, process and add to post.
    
    		$latitude = $arrXml["result"]["geometry"]["location"]["lat"];
    		$longitude = $arrXml["result"]["geometry"]["location"]["lng"];
    
    		//echo 'Post ID: '.$postID.'<br />';
    		//echo 'Status: '.$status.'<br />';
    		//echo 'Lat: '.$latitude.'<br />';
    		//echo 'Lng: '.$longitude.'<br />';
    
    		update_post_meta($postID, 'latitude' , $latitude);
    		update_post_meta($postID, 'longitude', $longitude);
    		die('Why do you save with die???');
    	}
    }
     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;
    }
    Thread Starter GHMedia

    (@ghmedia)

    Ya, I have no idea why when I issue the die() it saves it, but if I don’t and just let it finish there is nothing. Frustrating, It is like it’s flushing something that needs to be flushed. Thanks for the help and guidance Craig!

    Thread Starter GHMedia

    (@ghmedia)

    Hi Dave,
    Yes it is called from that function, does the Geoencoding, then updates the post_meta. At that point it exits. If I put a die() there it actually updates the post_meta, if I don’t have the die it goes back to the edit screen and nothing gets saved.
    die() must be doing some cleanup that I need to do, just don’t know what.
    Thanks again
    Gavin

    Thread Starter GHMedia

    (@ghmedia)

    Ok, new problem. The custom post fields are not being updated unless I die(); in my functions.php
    Is there something I need to call after this in order to commit it it to the DB?

    update_post_meta($postID, 'latitude' , $latitude);
     update_post_meta($postID, 'longitude', $longitude);

    If I follow with a die(); it will update the post, if I don’t it doesn’t? WTH??

    Thanks,
    Gavin

    Thread Starter GHMedia

    (@ghmedia)

    Ok, new problem. The custom post fields are not being updated unless I die(); in my functions.php
    Is there something I need to call after this in order to commit it it to the DB?

    update_post_meta($postID, 'latitude' , $latitude);
    		update_post_meta($postID, 'longitude', $longitude);

    If I follow with a die(); it will update the post, if I don’t it doesn’t? WTH??

    Thanks,
    Gavin

    Thread Starter GHMedia

    (@ghmedia)

    That one I did already do Craig, I do have it’s own namespace, want to keep the conflicts to a minimum! Thanks again for the tips!

    Thread Starter GHMedia

    (@ghmedia)

    UGH!!!! Thanks Craig! That just kills me! Spent ALL day yesterday trying to figure out what the hell I did wrong, only to find nothing! 🙂
    I appreciate your input and help! Means a lot to me!
    Gavin

    Thread Starter GHMedia

    (@ghmedia)

    No, and using the new it never fires either. This is driving me nucking futs! I don’t get why it never fires. Not on localhost or on a hosted site.
    This is the new code, and still no joy, I am at a total loss.

    function on_post_publish( $ID, $post ) {
      if ('project' == $post->type) {
        echo '<script type="text/javascript">alert("This got called, project");</script>';
      }
    }
    add_action('publish_post', 'on_post_publish', 10, 2);

    Thread Starter GHMedia

    (@ghmedia)

    Craig,
    Wouldn’t just the publish_post hook any and all postings, both standard as well as CPT?
    Thanks,
    Gavin

    Thread Starter GHMedia

    (@ghmedia)

    Thanks Tara,
    Right now I have a custom post type for projects. The client will be entering details along with the address of the location. When the post is first published I want to Geoencode the address to the LAT and LON for the address. So I am trying to hook when the post is first published to fire off the code to do the encoding. No mater what I do I can’t get it to run when the post is saved. I have tried about 20 different ways to do it and nothing works.
    My last attempt was this, and still nothing!

    function on_post_publish( $strOldStatus, $strNewStatus, $post) {
        // A function to perform actions when a post is published.
    	if( ( $strOldStatus === 'draft' || $strOldStatus === 'auto-draft' ) && $strNewStatus === 'publish' ) {
    		echo '<script type="text/javascript">alert("This was a NEW post");</script>';
    	}
    	if(  $strOldStatus === 'publish' && $strNewStatus === 'publish' ) {
    		echo '<script type="text/javascript">alert("This was an UPDATE to a post");</script>';
    	}
    }
    add_action(  'transition_post_status',  'on_post_publish', 10,3);

    Thanks,
    Gavin

Viewing 11 replies - 1 through 11 (of 11 total)