Support

Account

Home Forums General Issues Help with acf/save_post – REST API to ACF v2

Solving

Help with acf/save_post – REST API to ACF v2

  • Hi,

    Hopefully this issue is straightforward to resolve.

    I’ve got a powershell script that posts to my WP installation via REST API which works great. It creates a new post in one post request, then a second post request is called to add data to ACF fields with the help of airesvsg’s REST API to ACF v2 plugin.

    Now what I need is once the ACF fields are updated (on the second request), for an email to be fired out.

    When using save_post, it works fine and triggers an email, although as this doesn’t yet contain ACF data, nothing goes out.

    Now when using acf/save_post nothing happens until I actually go into the post and click update. Looking at the REST API to ACF github and docs, there’s nothing that specifies if I should be using a different action.

    Any ideas?

    Code below:

    	function email_alerts($post_id) {
    		if( get_field('enabled','options') ) {
    			if( get_field('email',$post_id)) {
    				
    				$e_status1 	= 	get_field('primary_online',$post_id);
    				$e_status2 	= 	get_field('primary_active',$post_id);
    				$e_status3 	= 	get_field('backup_online',$post_id);
    				$e_status4 	= 	get_field('backup_active',$post_id);
    				$e_datetime = 	get_field('date_time',$post_id);
    				$e_brand	= 	get_field('brand',$post_id);
    				$e_siteid 	= 	get_field('site_id',$post_id);
    				$e_ip 		=	get_field('ip',$post_id);
    				$e_url		=	get_site_url();
    				$e_site		=	get_the_title( $post_id );
    				$e_alerts 	= 	get_field('alerts','options');
    			
    			
    				if( $e_alerts && in_array('all', $e_alerts) && $e_status1 = "False" && $e_status3 = "False" ) {
    					$subject1 = 'System Alert - ' . $e_siteid . ' ' . $e_site . ' - Primary and Backup Offline';
    					$body1 = 'This is an automated alert sent from ' . $e_url . ' regarding ' . $e_brand . ' ' . $e_site;
    					$email1 = get_field('email_recipients', 'options');
    					$headers1 = array('Content-Type: text/html; charset=UTF-8','From: Someone <[email protected]>');
    					wp_mail($email1, $subject1, $body1, $headers1 );								
    				} 
    				
    				if( $e_alerts && in_array('pri', $e_alerts) && $e_status1 = "False" && $e_status3 = "True") {
    					$subject2 = 'System Alert - ' . $e_siteid . ' ' . $e_site . ' - Primary Offline';
    					$body2 = 'This is an automated alert sent from ' . $e_url . ' regarding ' . $e_brand . ' ' . $e_site;	
    					$email2 = get_field('email_recipients', 'options');
    					$headers2 = array('Content-Type: text/html; charset=UTF-8','From: Someone <[email protected]>');
    					wp_mail($email2, $subject2, $body2, $headers2 );					
    				}
    				
    				if( $e_alerts && in_array('sec', $e_alerts) && $e_status3 = "True" && $e_status3 = "False") {
    					$subject3 = 'System Alert - ' . $e_siteid . ' ' . $e_site . '- Backup Offline';
    					$body3 = 'This is an automated alert sent from ' . $e_url . ' regarding ' . $e_brand . ' ' . $e_site;	
    					$email3 = get_field('email_recipients', 'options');
    					$headers3 = array('Content-Type: text/html; charset=UTF-8','From: Someone <[email protected]>');
    					wp_mail($email3, $subject3, $body3, $headers3 );				
    				}
    			
    			}
    		}
    	}	
    	add_action( 'acf/save_post', 'email_alerts', 20);
  • Hi deset,
    stuck with the same issue here… did you find a trick to avoid this problem?

  • I have the same thing using the acf/save_post. Field value saved only after i hit the Update button a second time.

  • No luck so far unfortunately. I emailed the REST to ACF plugin author (who replied very quickly!) and he confirmed there currently isn’t an action to indicate an item was saved. His only suggestion was a filter that runs prior to saving ACF.

    add_action('acf/rest_api/post/prepare_item', function($item, $request){
    	// do something	
    }, 10, 2);
    

    Not too sure if that would help anyone else – if someone finds a creative solution please let me know!

    For the time being, I’m thinking of just setting up a CRON job to email every x amount of minutes/hours.

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

The topic ‘Help with acf/save_post – REST API to ACF v2’ is closed to new replies.