Support

Account

Home Forums Backend Issues (wp-admin) acf/save_post not working

Solving

acf/save_post not working

  • I’m not sure why, but the following isn’t outputting anything to error_log. Error output is definitely working, as I’ve managed to output using the post_updated action. So, for some reason this action is not working, even though I am changing the value in fields and updating the page clicking the page’s update button. Could it be that it’s a page, not a post? I’m using the latest versions of ACF and WP.

    add_action('acf/save_post', 'my_acf_save_post');
    function my_acf_save_post( $post_id ) {
    
      // Get newly saved values.
      $values = get_fields( $post_id );
      error_log($post_id);
    
    }
  • Is this added to a file that is always loaded, like functions.php?

    Is there anything for ACF to save? There are fields being submitted? If there are no ACF fields submitted the acf action will never fire.

    
    function acf_save_post( $post_id = 0, $values = null ) {
    	
    	// Override $_POST data with $values.
    	if( $values !== null ) {
    		$_POST['acf'] = $values;
    	}
    	
    	// Bail early if no data to save.
    	if( empty($_POST['acf']) ) {
    		return false;
    	}
    	
    	// Set form data (useful in various filters/actions).
    	acf_set_form_data( 'post_id', $post_id );
    	
    	// Filter $_POST data for users without the 'unfiltered_html' capability.
    	if( !acf_allow_unfiltered_html() ) {
    		$_POST['acf'] = wp_kses_post_deep( $_POST['acf'] );
    	}
    	
    	// Do generic action.
    	do_action( 'acf/save_post', $post_id );
    	
    	// Return true.
    	return true;
    }
    
  • Yes to the file question (the update post hook is working in the same file).

    Regarding the other question, there are definitely ACF fields in the page that I’m updating. However, interestingly enough, if I try to error_log the $_REQUEST or $_POST arrays in the post_updated hook I have set up, all I get is something like user_locale.

  • I think you have an issue elsewhere and not in ACF, unfortunately, from here I don’t know where I would start looking, but it seems that something is eliminating the ACF fields before ACF is run.

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

You must be logged in to reply to this topic.