Support

Account

Home Forums Front-end Issues Getting form data before saving

Helping

Getting form data before saving

  • At this point I can create Form_Fields add them to a custom Post Type, list them on a custom Page Template and let signed_in users add posts to this custom Post Type.

    It’s all working great, all the custom fields are getting filled and show up in the back-end but is there a way to manipulate some fields just a sec. before adding them to the database?

    What I really want is people willing out the forms and when they press Publish I would like to create the Post Title based on the option they have selected.
    So pick a 5 selections – combine them and make a title out of them.

    All the becoming stuff in PHP I can do but how do you get the form-data before saving?

  • Hi @shmoo

    Thanks for the post.

    You can get the form data by hooking into the acf/pre_save_post filter and obtain the $_POST data.

    Here is an example of how this would look:

    <?php
    
    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
    
        }
    
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_title'  => $_POST['acf']['field_53bb9a2018722'] ,
            'post_type'  => 'post' ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.