Support

Account

Home Forums Front-end Issues Can't save_post on form submission

Solved

Can't save_post on form submission

  • I have a front end form that I would like to use to create a new post with custom fields. Here’s the function I use to register the form and save the post:

    // functions.php
    function dutchtown_poll_form( $args = array() )
    {
        $default_args = array(
            'fields'            => array(
                'field_5fb66cffb0492',
                'field_5fb66e2ff4e9d',
                'field_5fb6b393b30dc'
            ),
            'post_id'	    	=> 'new_post',
            'new_post'			=> array(
                'post_type'		=> 'poll_response',
                'post_status'   => 'publish',
                'post_title'    => 'Poll Response',
            ),
            'return'			=> home_url( 'ideas' ),
            'submit_value'		=> 'Submit your poll response'
        );
    
        $args = wp_parse_args( $args, $default_args );
        
        acf_register_form( $args );
    }
    
    function dutchtown_save_poll_response( $post_id )
    {
        if ( get_post_type( $post_id ) !== 'poll_response' ) : return; endif;
    	if ( is_admin() ) : return; endif;
        
    	$post = get_post( $post_id );
    
    	$name = get_field( 'name', $post_id );
        while ( have_rows( 'poll_answer', $post_id ) ) :
            the_row();
        	$answer[] = get_sub_field( 'answer', $post_id );
        endwhile;
    }
    
    add_action('acf/save_post', 'dutchtown_save_poll_response' );

    Then I call the form like so in my page template:

    // page-form.php
    
    acf_form_head();
    get_header();
    
    $args = array(
        'id' => 'poll-category',
    );
    
    dutchtown_poll_form( $args );   // register form
    acf_form( $poll_category );     // display form

    Everything is as expected except that the post is not created on submission, so I assume something is going wrong at acf/save_post.

    I do see the following notice when I turn on debug mode:

    Notice: Undefined offset: 1 in /app/public/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-helpers.php on line 4636
    Call Stack
    # Time Memory Function Location
    1 0.0004 420920 {main}( ) …/index.php:0
    2 0.0004 421192 require( ‘/app/public/wp-blog-header.php’ ) …/index.php:17
    3 0.4261 15923208 require_once( ‘/app/public/wp-includes/template-loader.php’ ) …/wp-blog-header.php:19
    4 0.4281 16009528 include( ‘/app/public/wp-content/themes/itaska/page-poll.php’ ) …/template-loader.php:106
    5 0.4281 16009528 acf_form_head( ) …/page-poll.php:12
    6 0.4281 16009528 acf_form_front->enqueue_form( ) …/form-front.php:624
    7 0.4281 16009528 acf_form_front->check_submit_form( ) …/form-front.php:340
    8 0.4281 16009528 acf_decrypt( ) …/form-front.php:379

    Line 12 on page-poll.php is where I call acf_form_head();

  • I think I got it. Instead of

    dutchtown_poll_form( $args ); // register form
    acf_form( $poll_category ); // display form

    I’ve set dutchtown_poll_form() to just return the registration values:

    $args = dutchtown_poll_form( $args ); // returns values rather than registering
    acf_form( $args );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.