0

I'm trying to find how to intercept the values in some custom fields on a page when they are being saved. I have searched and found out how to add a hook to the functions file to get the standard content fields, but so far haven't worked out how to do the same for custom fields. I'm using ACF Pro with flexible content fields, so the actual values come through as mutli-dimensional arrays.

The code that I am using is at the bottom. This code successfully modifies the standard 'post_content' field before saving. Basically I want to do the same for various custom fields. Any ideas?

function save_my_post( $content ) {
  global $post;
  if( isset($post) && get_post_type( $post->ID ) == 'post' ){
    $content['post_content'] = function_to_manipulate_the_content();
  }
  return $content;
}
add_filter( 'wp_insert_post_data', 'save_my_post' );
0

It all depends on how your "custom fields" are saved. If you store the values as post meta then you would have to call the update_post_meta function to update them on wp_insert_post_data. In the example below Im setting the post meta "my_meta_key" with the string value "my_meta_value".

function save_my_post( $content ) {
    global $post;
    if( isset($post) && get_post_type( $post->ID ) == 'post' ){
        update_post_meta( $post->ID, 'my_meta_key', 'my_meta_value' );
    }
    return $content;
}
add_filter( 'wp_insert_post_data', 'save_my_post' );
  • I'm afraid I don't know exactly how they are saved, I'm using ACF Pro so if anyone knows how that plugin saves its fields let me know. All I know is, when I print_r($_POST) the ACF fields are all in an array $_POST['acf']. All I want to do is change the content of those before saving. – Adam B Sep 16 '16 at 13:57
  • I see. If you want to intercept a custom field created by ACF there is a filter for just that called "acf/save_post". You can find some documentation on that att ACF's webpage: advancedcustomfields.com/resources/acfsave_post – Jesper Nilsson Sep 16 '16 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.