Support

Account

Home Forums General Issues Frontend Form not populating right in backend Reply To: Frontend Form not populating right in backend

  • AHHHHH!!!!! You rock! Thank you SO much! It’s now fixed!

    For anyone else who needs the complete code, here you go:

    Functions.php

    //Auto add and update Title field:
    function your_post_title_updater( $post_id ) {
        
        // get the field name
        $new_title_field_name = get_field('your_field_name', $post_id);
    
        // get the selected date and the current date
        $new_title_date = get_field('start_date', $post_id);
        $new_title_date = new DateTime($new_title_date);
        
        // set the title
        $new_title = $new_title_field_name . ' - ' . $new_title_date->format('M j, Y');
        
        $new_slug = sanitize_title( $new_title );
    
        // create the update data holder
    	$your_post = array();
    	$your_post['ID'] = $post_id;
    	$your_post['post_title'] = $new_title;
    	$your_post['post_name'] = wp_strip_all_tags($new_slug);
    
        //Unhook function to prevent infitnite looping
    	remove_action('acf/save_post', 'your_post_title_updater', 20);
    
    	// Update the post into the database
    	wp_update_post( $your_post );
    
    	//Rehook function to prevent infitnite looping
    	add_filter('acf/save_post', 'your_post_title_updater', 20);
    
    }
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'your_post_title_updater', 20);

    Your form display page

    <?php
    
    function my_kses_post( $value ) {
    	// is array
    	if( is_array($value) ) {
    		return array_map('my_kses_post', $value);
    	}
    	// return
    	return wp_kses_post( $value );
    }
    
    add_filter('acf/update_value', 'my_kses_post', 10, 1);
    	
    add_action( 'get_header', 'your_do_acf_form_head', 1 );
    function your_do_acf_form_head() {
        // Bail if not logged in or not able to post
        if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
            return;
        }
        acf_form_head();
    }
    
    add_action( 'wp_print_styles', 'your_deregister_admin_styles', 999 );
    function your_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' );
    }
     
    get_header(); ?>
    
    <div  class="page-wrapper">
    	<div class="row">
    		<div id="content">
    			
    			<?php /* The loop */
    				if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
            echo '<p>You must be a registered author to post.</p>';
            return;
        }
        
        $your_post = array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> wp_strip_all_tags($new_title),
    		'post_content'	=> false,
    		'form' => true,
    		'new_post'		=> array(
    			'post_type'		=> 'your_post_type',
    			'post_status'	=> 'publish'
    		),
    		'field_groups'       => array(2793),
    		'return'             => '%post_url%',
    		'submit_value'       => 'Submit',
    		'updated_message'    => 'Saved!',
    		'post_title'    => wp_strip_all_tags($new_slug),
    	);
    	
    	acf_form( $your_post );
    
    		</div><!-- #content -->
    	</div><!-- #row -->
    </div><!-- #page-wrapper -->
    
    <?php acf_enqueue_uploader(); ?>
    
    <script type="text/javascript">
    (function($) {
    	
    	// setup fields
    	acf.do_action('append', $('#popup-id'));
    	
    })(jQuery);	
    </script>
    
    <?php get_footer(); ?>