Support

Account

Home Forums Backend Issues (wp-admin) Using acf/save_post publish the content when doing a preview

Helping

Using acf/save_post publish the content when doing a preview

  • I want to use the “acf/save_post” action to sanitize some fields that contains html, and I am able to make it work. However, when I try to use “preview”-button in wp-admin it seems to do a publish as well. If I remove the “acf/save_post” action the preview work as I expect, the content is not published.

    Am I doing something wrong or is this a bug?

    function my_acf_save_post( $post_id ) {

    if ( empty( $_POST[‘acf’] ) ) {
    return;
    }

    $fields = $_POST[‘acf’][‘a_field_key’]; //flex content

    //doing something with the fields

    //update the fields
    $return = update_field( ‘a_field_key’, $fields );

    }

    // run after ACF saves the $_POST[‘acf’] data
    add_action( ‘acf/save_post’, ‘my_acf_save_post’, 20 );

  • Got the solution from ACF support:

    “You don’t need to use update_field at all, instead, you should hook into the acf/save_post action BEFORE ACF (priority less than 10) and simply change the $_POST[‘acf’] data. That way, you don’t need to run the update_field function at all, ACF will do that for you an ensure it is saved to the correct post/revision.”

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

The topic ‘Using acf/save_post publish the content when doing a preview’ is closed to new replies.