Support

Account

Home Forums Front-end Issues Repeater and Flexible Fields not Saving Values from FrontEnd

Solved

Repeater and Flexible Fields not Saving Values from FrontEnd

  • Hello,
    I’m not able to save all fields from frontend.

    In particular I can’t save repeater fields containing flexible layout fields.

    I’m using ACF Pro

    This is my custom page template for the frontend posting.

    /**
     * Template Name: Create Recipe Frontend
     */
     
     
    function can_user_post_recipe(){
    	$return = false;
    	if ( ( is_user_logged_in() && current_user_can('read') ) ) {
    		$return = true;
    	}
    	return $return;
    } 
     
    
    /**
     * Deregister the admin styles outputted when using acf_form
     */
    // add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
    function tsm_deregister_admin_styles() {
    
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
    		return;
    	}
    	wp_deregister_style( 'wp-admin' );
    }
    
    /**
     * Back-end creation of new candidate post
     */
    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 ( ! ( can_user_post_recipe() ) ) {
    		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'     => 'post', 
    		'post_status'   => 'pending', 
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_559a8e6366915']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_559a8e7b66916'], // Post Content ACF field key
    	);
    
    	$post_id = wp_insert_post( $post );
    
    	do_action( 'acf/save_post' , $post_id );
    
    	return $post_id;
    
    }
    
    /**
     * Save ACF image field to post Featured Image
     */
    add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
    function tsm_save_image_field_to_featured_image( $post_id ) {
    
    	// Bail if not logged in or not able to post
    	if ( ! ( can_user_post_recipe() ) ) {
    		return;
    	}
    
    	// Bail early if no ACF data
    	if( empty($_POST['acf']) ) {
    		return;
    	}
    
    	// ACF image field key
    	$image = $_POST['acf']['field_559a8eb766918'];
    
    	// Bail if image field is empty
    	if ( empty($image) ) {
    		return;
    	}
    
    	// Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	add_post_meta( $post_id, '_thumbnail_id', $image );
    
    }
    
    add_filter('acf/update_value', 'wp_kses_post', 10, 1); 
    acf_form_head();
    get_header(); ?>
    
    	<div id="primary" class="site-content row" role="main">
    
    		<div class="col grid_8_of_12">
    
    			<?php if ( have_posts() ) : ?>
    
    				<?php while ( have_posts() ) : the_post(); ?>
    					<?php // get_template_part( 'content', 'page' ); ?>
    					<?php 
    
    							
    							// Bail if not logged in or able to post
    							if ( ! ( can_user_post_recipe() ) ) {
    								echo '<p>You must be a registered author to post.</p>';
    								
    							} else {
    
    								$new_post = array(
    									'id'      			 => 'frontend-recipe',
    									'post_id'            => 'new', 
    									'field_groups'       => array(224405,224409), 
    									'form'               => true,
    									'return'             => '/inserisci-ricetta/ricetta-inserita/',
    									'html_before_fields' => '',
    									'html_after_fields'  => '',
    									'submit_value'       => 'Invia Ricetta',
    									'updated_message'    => 'Salvato!',
    									'uploader' 			 => 'basic'
    								);
    								
    								echo acf_form( $new_post );
    								
    							}
    
    					?>
    				<?php endwhile; ?>
    
    			<?php endif; ?>
    
    		</div> 
    		<?php get_sidebar(); ?>
    
    	</div> 
    
    <?php get_footer(); ?>
  • I have an update, looking in db, I can see that the repeater fields are saved, but they are just empty, this is a screenshot: http://prntscr.com/7r2jjm

    In screenshot, the repeater field is “recipe_details”, this field contains severao flexible layout fields, that are not saved, infact the metavalus is just an empty serialized string -> a:2:{i:0;s:1:”A”;i:1;s:1:”A”;}

    I really can’t save this flexible fields contained in repeater field.
    Nobody has already did that?

  • New update:

    I tried using a more simple field group, containing just
    – a repeter field of text fields
    – a flexible content field with 2 fields: text and image

    for both I have the same issue: fields are created in database, infact I see rows in backend edit post, but this rows do not have any value inside. they are just empty.

  • Further update.

    debugging the form data with chrome dev tools, it looks like the values are sent, but for some reason they are not saved in db

    here it is the admin-ajax.php form data sent in headers

    _acfnonce:d1c835265a
    _acfchanged:1
    _acf_form:eyJpZCI6ImZyb250ZW5kLXJlY2lwZSIsInBvc3RfaWQiOiJuZXciLCJuZXdfcG9zdCI6ZmFsc2UsImZpZWxkX2dyb3VwcyI6WyJncm91cF81NTllOWVkYjQwZmNhIiwiZ3JvdXBfNTU5ZTllZGI1Yzg1ZCJdLCJmaWVsZHMiOmZhbHNlLCJwb3N0X3RpdGxlIjpmYWxzZSwicG9zdF9jb250ZW50IjpmYWxzZSwiZm9ybSI6dHJ1ZSwiZm9ybV9hdHRyaWJ1dGVzIjp7ImlkIjoicG9zdCIsImNsYXNzIjoiIGFjZi1mb3JtIiwiYWN0aW9uIjoiIiwibWV0aG9kIjoicG9zdCJ9LCJyZXR1cm4iOiJodHRwOlwvXC93d3cubm9ubmFwYXBlcmluYS5pdFwvaW5zZXJpc2NpLXJpY2V0dGFcLz91cGRhdGVkPXRydWUiLCJodG1sX2JlZm9yZV9maWVsZHMiOiIiLCJodG1sX2FmdGVyX2ZpZWxkcyI6IiIsInN1Ym1pdF92YWx1ZSI6IkludmlhIFJpY2V0dGEiLCJ1cGRhdGVkX21lc3NhZ2UiOiJTYWx2YXRvISIsImxhYmVsX3BsYWNlbWVudCI6InRvcCIsImluc3RydWN0aW9uX3BsYWNlbWVudCI6ImxhYmVsIiwiZmllbGRfZWwiOiJkaXYiLCJ1cGxvYWRlciI6ImJhc2ljIn0=
    acf[field_559a8e6366915]:This is the post title
    acf[field_559a8e7b66916]:this is the post content
    acf[field_559a8eb766918]:C:\fakepath\80_IMG_2650.jpg
    acf[field_5368b65e4cd39]:1
    acf[field_5383505f17035]:this is another wysiwyg field
    acf[field_5368bdb913e74]:
    acf[field_5368bdb913e74][acfcloneindex][acf_fc_layout]:recipe_tot_time
    acf[field_5368bdb913e74][acfcloneindex][field_5368beb2078cc]:
    acf[field_5368bdb913e74][acfcloneindex][field_5368bf64078cd]:
    acf[field_5368bdb913e74][acfcloneindex][field_536cfd2d6fa7a]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfd516fa7b]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfdab6fa7c]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfb9f6fa76]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfbfb6fa77]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfc516fa78]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfeb06fa7f]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfed36fa80]:0
    acf[field_5368bdb913e74][acfcloneindex][field_536cfef46fa81]:0
    acf[field_5368bdb913e74][559fe5a1e1e57][acf_fc_layout]:recipe_dosi
    acf[field_5368bdb913e74][559fe5a1e1e57][field_5368beb2078cc]:5
    acf[field_5368bdb913e74][559fe5a1e1e57][field_5368bf64078cd]:people
    acf[field_5368bdb913e74][559fe5a7e1e58][acf_fc_layout]:recipe_prep_time
    acf[field_5368bdb913e74][559fe5a7e1e58][field_536cfd2d6fa7a]:0
    acf[field_5368bdb913e74][559fe5a7e1e58][field_536cfd516fa7b]:0
    acf[field_5368bdb913e74][559fe5a7e1e58][field_536cfdab6fa7c]:30
    acf[field_5368bdb913e74][559fe5afe1e59][acf_fc_layout]:recipe_cook_time
    acf[field_5368bdb913e74][559fe5afe1e59][field_536cfb9f6fa76]:0
    acf[field_5368bdb913e74][559fe5afe1e59][field_536cfbfb6fa77]:0
    acf[field_5368bdb913e74][559fe5afe1e59][field_536cfc516fa78]:20
    acf[field_5368bdb913e74][559fe5b2e1e5a][acf_fc_layout]:recipe_tot_time
    acf[field_5368bdb913e74][559fe5b2e1e5a][field_536cfeb06fa7f]:0
    acf[field_5368bdb913e74][559fe5b2e1e5a][field_536cfed36fa80]:0
    acf[field_5368bdb913e74][559fe5b2e1e5a][field_536cfef46fa81]:50
    acf[field_53694b8b14a44]:
    acf[field_53694b8b14a44][acfcloneindex][acf_fc_layout]:recipe_ingredient
    acf[field_53694b8b14a44][acfcloneindex][field_5369557ce3365]:
    acf[field_53694b8b14a44][acfcloneindex][field_53694c0014a45]:
    acf[field_53694b8b14a44][acfcloneindex][field_53694c8514a46]:
    acf[field_53694b8b14a44][acfcloneindex][field_53694cdd14a47]:
    acf[field_53694b8b14a44][acfcloneindex][field_53694cf514a48]:
    acf[field_53694b8b14a44][559fe5b7e1e5b][acf_fc_layout]:recipe_ingredient
    acf[field_53694b8b14a44][559fe5b7e1e5b][field_53694c0014a45]:1
    acf[field_53694b8b14a44][559fe5b7e1e5b][field_53694c8514a46]:spoon
    acf[field_53694b8b14a44][559fe5b7e1e5b][field_53694cdd14a47]:sugar
    acf[field_53694b8b14a44][559fe5b7e1e5b][field_53694cf514a48]:
    acf[field_53694b8b14a44][559fe5c1e1e5c][acf_fc_layout]:recipe_ingredient
    acf[field_53694b8b14a44][559fe5c1e1e5c][field_53694c0014a45]:200
    acf[field_53694b8b14a44][559fe5c1e1e5c][field_53694c8514a46]:ml
    acf[field_53694b8b14a44][559fe5c1e1e5c][field_53694cdd14a47]:water
    acf[field_53694b8b14a44][559fe5c1e1e5c][field_53694cf514a48]:
    acf[field_536954f9e3362]:
    acf[field_536954f9e3362][acfcloneindex][acf_fc_layout]:recipe_process_step
    acf[field_536954f9e3362][acfcloneindex][field_53695987027e2]:
    acf[field_536954f9e3362][acfcloneindex][field_5369588b92009]:
    acf[field_536954f9e3362][acfcloneindex][field_536958c19200a]:
    acf[field_536954f9e3362][559fe5d5e1e5d][acf_fc_layout]:recipe_process_step
    acf[field_536954f9e3362][559fe5d5e1e5d][field_5369588b92009]:pur sugar in water
    acf[field_536954f9e3362][559fe5d5e1e5d][field_536958c19200a]:
    acf[field_536954f9e3362][559fe5dce1e5e][acf_fc_layout]:recipe_process_step
    acf[field_536954f9e3362][559fe5dce1e5e][field_5369588b92009]:mix a lot and drink
    acf[field_536954f9e3362][559fe5dce1e5e][field_536958c19200a]:
    acf[field_53695a0dcfde2]:this is yet another wysiwyg field
    action:acf/validate_save_post

    PS: what is “acfcloneindex”?

  • In your pre save post filter

    
    /**
     * Back-end creation of new candidate post
     */
    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 ( ! ( can_user_post_recipe() ) ) {
    		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'     => 'post', 
    		'post_status'   => 'pending', 
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_559a8e6366915']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_559a8e7b66916'], // Post Content ACF field key
    	);
    
    	$post_id = wp_insert_post( $post );
    
    	do_action( 'acf/save_post' , $post_id );
    
    	return $post_id;
    
    }
    

    Why are you doing do_action( 'acf/save_post' , $post_id );? Did you read somewhere that you should add that? Because it is not in any of the documentation or tutorials on acf/pre_save_post.

    Try removing that line.

  • Actually I tried to follow this article:
    http://thestizmedia.com/front-end-posting-with-acf-pro/

    anyway I keep having the same issue even removing that line.

    Lately I tried also assign to the post a category in the pre_save_post, adding this line
    wp_set_object_terms($post_id, array(1829), ‘category’);

    right after
    add_post_meta( $post_id, ‘_thumbnail_id’, $image );

    but it works randomly, sometimes the category is assigned, sometime not.

    I’ve also moved all the code (except the acf_form part) from page template to theme funcions.php, but nothing’s changed.

    Problem is I need this to work asap, after losing 2 days on this I’m ready to pay someone to fix it. Anyone available?

  • I would remove the acf/save_post filter from the code entirely. Everything you’re doing there should be done in the pre_save_post filter.

    I did see another error in the pre_save_post filter, but I’m not sure that would effect anything either.

    
    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 ( ! ( can_user_post_recipe() ) ) {
                    // added $post_id here
    		return $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_type'     => 'post', 
    		'post_status'   => 'pending', 
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_559a8e6366915']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_559a8e7b66916'], // Post Content ACF field key
    	);
    
    	$post_id = wp_insert_post( $post );
    
    	// more stuff from save post filter to here
    
    	// ACF image field key
    	$image = $_POST['acf']['field_559a8eb766918'];
    
    	// Bail if image field is empty
    	if ( empty($image) ) {
                    // added $post_id to return
    		return $post_id;
    	}
    
    	// Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	add_post_meta( $post_id, '_thumbnail_id', $image );
    
    	return $post_id;
    
    }
    

    Other than that, I don’t see anything wrong with your code. If you make these changes and it’s still not working then you may have something else that’s causing some interference.

  • I tried like you suggest.

    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 ( ! ( can_user_post_recipe() ) ) {
    		return $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_type'     => 'post', 
    		'post_status'   => 'pending', 
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_559a8e6366915']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_559a8e7b66916'], // Post Content ACF field key
    	);
    
    	$post_id = wp_insert_post( $post );
    	
    	wp_set_object_terms($post_id, array(1829), 'category');
    	
    	// ACF image field key
    	$image = $_POST['acf']['field_559a8eb766918'];
    	
    	// Bail if image field is empty
    	if ( empty($image) ) {
    		return $post_id;
    	}
    
    	// Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	add_post_meta( $post_id, '_thumbnail_id', $image );
    
    	return $post_id;
    	
    }

    Now the wp_set_object_terms seems to work nicely, but the thumbnail is not saved at all, and the original issue is still there: no repeater or flexible field saved in db.

    Maybe I did not say this: in backend everything is working fine (since a lot of time)

    I can’t see any error, not js in console, not php in server log.

  • 1) Do you have any other acf filters anywhere in your code?

    2) Does this field group save using the exact same inputs on the back end? Add them to a post and enter the same information you’re entering on the front end.

    3) How many fields are there, do the beginning fields save while the later fields are the ones not being saveed?

  • 1) no other filter, just frontend templates

    2) As you can see I use 2 field groups in the acf_form. The first one is made just for frontend form (is where I have post title, content and featured image), the second one (where I have the issue) is the very same I use in backend.

    3) IN the 2nd field group I have 6 fields, in this order:
    – true/false field (true is the condition for all the following fields to show) – working
    – wysiwyg field – working
    – flexible content field (inside there are 4 layouts, with a total of 11 subfields) – not working
    – flexible content field (inside there are 2 layouts with a total of 5 subfields) – Not working
    – flexible content field (inside there are 2 layouts with a total of 3 subfields) – Not working
    – wysiwyg field – working

    If you want to try, I’ve exported both groups in this zip file https://dl.dropboxusercontent.com/u/3261727/field-groups.zip

    PS: I’ve tried not to use the second group (the complex one) and using another one just with 1 repeater field with only one text subfield, same problem: data is sent from the form, rows are created in backend but values are empty, infact in database I can see serialized strings like this one: a:2:{i:0;s:1:”A”;i:1;s:1:”A”;}

  • Been testing.

    I added your field groups to one of my test sites

    I set the location for both field groups to appear on posts

    I created a font end form that shows on all pages

    I added this following the the top of my page template

     
    add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
    function tsm_do_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_type'     => 'post', 
    		'post_status'   => 'pending', 
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_559a8e6366915']), // Post Title ACF field key
    		'post_content'  => $_POST['acf']['field_559a8e7b66916'], // Post Content ACF field key
    	);
    
    	$post_id = wp_insert_post( $post );
    	
    	wp_set_object_terms($post_id, array(1829), 'category');
    	
    	// ACF image field key
    	$image = $_POST['acf']['field_559a8eb766918'];
    
    	// Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	add_post_meta( $post_id, '_thumbnail_id', $image );
    
    	return $post_id;
    	
    }
     
    acf_form_head();
    
    get_header(); ?>
    

    And I used the following to show the form, I reduced this to just what was needed to test it.

    
    $new_post = array(
    	'post_id'            => 'new', 
    	'field_groups'       => array(4,8), // different id values here
    	'uploader' 			 => 'basic'
    );
    
    acf_form( $new_post );
    

    Notes, I did not use the location rules to only show the first group for admins, just showed it on the page.

    I then went to a front end page and added data to every field, I created serveral flex fields. Everything was saved to a new post as expected.

    I used an site that only has ACF installed, no other plugins. I was using the 2014 theme.

    There must be something else causing the issue of the content not saving.

    Deactivate other plugins and see it that helps. If not try switching to a defualt WP theme and see if that helps (you”ll need to add your form code to the default theme)

  • John You’re awesome.

    I think the key is putting the ‘acf/pre_save_post’ filter before:

    acf_form_head();
    get_header();

    but I still have 1 issue: featured image is not saving.

    maybe the ‘acf/save_post’ action is needed fo this like I saw in that article I linked before?

  • I think you may be right.

    The problem is that you need the attachment/image ID in order to set the post thumbnail.

    But instead of do_action, in your pre save post function change it to:

    add_action('acf/save_post', 'tsm_save_image_field_to_featured_image', 20);

    I’ve set the priority to 20 above so it runs after the acf save post action and ACF has saved all of the custom fields.

    
    function tsm_save_image_field_to_featured_image( $post_id ) {
    
      // this function will only be called if the pre_save_post
      // filter was run because that's where we added the hook
      // so you can remove all the tests
      // I am using get_post_meta here to make sure
      // we get the ID of the image field no matter what you have
      // set the image field to return (URL, ID or Object)
      $attachment_id = intval(get_post_meta($post_id, 'your_image_field', true));
      if ($attachment_id) {
        set_post_thumbnail($post, $attachment_id);
      }
    
  • I tried to use the add_action filter I was already using in the first version, but without the do_actino in the acf/pre_save_post filter.

    It looks like it works, but I’m still using

    add_post_meta( $post_id, '_thumbnail_id', $image );

    instead of yours

    set_post_thumbnail($post, $attachment_id);

    do you suggest to use the second one?

  • If the first one works, I’d leave it alone.

  • ok, you’re right 🙂

  • Hi bluantinoo!

    I had a similar problem and found this thread through your comment of the tutorial the code is based on.

    Would you mind to post the whole working code? I tried to follow all your comments but at the end I got confused and couldnt get it work.

    Are you using Genesis as well?

    Thanks!

  • Hey there! I had same issue. I had checkbox group, and if that checkbox was checked, there apear repeatable field. The problem was I had no labels for those fields, not for checkbox field and for repeatable field neither… That’s a bug I guess.. My ACF Pro ver. 5.9.3. May that will help anyone. I’m glad, I’ve found a solution for my problem.

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

The topic ‘Repeater and Flexible Fields not Saving Values from FrontEnd’ is closed to new replies.