Support

Account

Home Forums Front-end Issues ACF submit button does not create a post

Helping

ACF submit button does not create a post

  • Hello. I have managed to create custom field types using ACF Pro and used a couple of tutorials to place the form fields on a front-end page to gather user data and store it on the database. The form displays fine and even validates ‘required’ fields if i leave them empty but when i click the submit button nothing happens. no page reload, nothing! i will post my code below. PLEASE HELP. I NEED THIS.

    Page_template.PHP

    acf_form_head();
    
    add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
    function tsm_deregister_admin_styles() {
    	// Bail if not logged in or not able to post
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    	wp_deregister_style( 'wp-admin' );
    }
    
    <div class="add_feed_form"><?php 
    
    			// Bail if not logged in or able to post
    				    if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
    				        echo 'You must be a registered author to post.
    ';
    				        return;
    				    }
    				 
    				    $new_post = array(
    				        'post_id'            => 'new', // Create a new post
    				        
    				        'field_groups'       => array(4195), // Create post field group ID(s)
    				        'form'               => true,
    				        'return'             => '%post_url%', // Redirect to new post url
    				        'html_before_fields' => '',
    				        'html_after_fields'  => '',
    				        'submit_value'       => 'Publish feed',
    				        'updated_message'    => 'Feed Successfully created!'
    				    );
    				    acf_form( $new_post );
    
    ?> 
    				
    				</div> <?php //end citizen form div ?>

    functions.php

    <?php 
    
    /**
     * Back-end creation of new candidate post
     * @uses Advanced Custom Fields Pro
     */
    add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
    
    function tsm_do_pre_save_post( $post_id ) {
     
        // Bail if not logged in or not able to post
        if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
            return;
        }
     
        // check if this is to be a new post
        if( $post_id != 'new' ) {
            return $post_id;
        }
     
        // Create a new post
        $post = array(
            'post_type'     => 'citizen_feed', // Your post type ( post, page, custom post type )
            'post_status'   => 'publish', // (publish, draft, private, etc.)
            'post_title'    => wp_strip_all_tags($_POST['acf']['field_570e19d9c34b5']), // Post Title ACF field key
            'post_content'  => $_POST['acf']['field_570e189cc34b4'], // Post Content ACF field key
        );
    
    // insert the post
        $post_id = wp_insert_post( $post );
     
        // Save the fields to the post
        do_action( 'acf/save_post' , $post_id );
     
        return $post_id;
     
    }
    
    ?>
  • The first thing that I see by looking at your code is where you are calling acf_form_head();

    This needs to be called before get_header();, so you’ll need to move acf_form_head(); to the template that’s calling the WP function.

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

The topic ‘ACF submit button does not create a post’ is closed to new replies.