• Please help! I can not for the life of me get this to work, it never fires. I am trying to create a function to do something when a post is first published.
    This code never seems to get called:

    function on_post_publish( $ID, $post ) {
        // A function to perform actions when a post is published.
    	echo '$ID: ';
    	var_dump($ID);
    
    	echo '$post: ';
    	var_dump($post);
    
    	echo '<script type="text/javascript">alert("Made it to the function!");</script>';
    	}
    add_action(  'publish_post',  'on_post_publish', 10, 2 );
Viewing 5 replies - 16 through 20 (of 20 total)
  • 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!

    Gavin, can you post the code? A die() call does not do anything but DIE, aka: stops script execution immediately, hence its name 🙂 There’s no “cleanup” that you should need to do. I suspect something happening after those function calls is causing the problem.

    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;
    }

    Gavin, can you try removing the do_action(‘save_post’) from within the nbpe_publish_project() function? I think is probably causing your issues.

    WordPress already has that hook defined elsewhere in core, obviously. The fact that it has TWO of them on submission is probably causing some issues, if I had to guess. It’s also probably why your die() is letting you see the meta values “work” right after saving them, because the second save_post never gets fired as WP continues executing the submission.

    Unless I’m not understanding something, I think you should be safe to remove that. Let me know how it goes. 🙂

    Hey Gavin,

    The following code is working perfectly fine for me, which is just a much simpler version of yours WITHOUT the save_post do_action() and conditionals (and obviously your custom fields). Doesn’t necessarily solve your issue just yet but able to narrow it down a bit further and make sure this is the proper hook for the job. I think Dave may be on to something with the extra save_post hook causing some issues.

    add_action('transition_post_status','nbpe_publish_project',10,3);
    
    function nbpe_publish_project($new_status,$old_status,$post){
    	nbpe_geoencode($post,$post->ID);
     }
    
    function nbpe_geoencode($post,$postID){
            update_post_meta( $postID, '_wp_page_template', '404.php' );
    }
Viewing 5 replies - 16 through 20 (of 20 total)

The topic ‘publish_post not working’ is closed to new replies.