Support

Account

Forum Replies Created

  • I could always generate the array after save and then delete the repeater, but it seems a bit messy :

    add_action('save_post', 'save_repeater_as_array');
    function save_repeater_as_array($post_id) {
    	$repeater = get_field('my_repeater', $post_id);
    	$json = json_encode($repeater);
    	update_field('json',$json,$post_id);
    	
    	if (!empty($repeater)) {
    		while (delete_row('my_repeater', 1, $post_id));
    	}
    }

    Also, I’ll have to rebuild the repeater on page load with JS, but that’s okay I don’t mind putting in the work.

  • Hi,
    Could you please provide more details? What if my layout is called ‘form’, how do I get the corresponding label? I tried several things without success.
    Thank you

  • Hi,

    Sorry for the triple-post. I tried to delete them but they weren’t appearing on the thread (even tried in incognito mode).
    Anyway, i figured right after posting that from your previous code :

    foreach ($the_row as $field_key => $value) {
        $field = get_field_object($field_key);
        // now get the label and value and whatever else you need from $field;
      }

    I just had to use
    $value

    Thank you

  • Actually I’m still struggling… I managed to get the label easily, but I still cant get the value.

    $subfield[‘value’] doesn’t seem to work.
    I also tried get_field($subfield[‘name’]) but it doesn’t work either.

    I’m simply trying to receive an email with all the field labels and values when a user submits a form.

    Here is my full function :

    add_filter('acf/save_post', 'create_post_from_form');  
    
    function create_post_from_form($post_id) {	
    
    	$to = '[email protected]';
    			
    	$subject = 'test notification 8';
    	$message = '';
    		
    	$fields = get_field_objects($post_id);
    		
    	foreach($fields as $field) {
    			
    		$message =  $message . ' <strong>' . $field['label'] . '</strong><br>';	
    		$key = $field['key'];
    			
    		while (have_rows($key, $post_id)) {
    			// always happens once on a group field
    			$the_row = the_row(); // the row returns the row
    			// $the_row will be an array of "field_key" => "value" pairs
    			foreach ($the_row as $field_key => $value) {
    				$subfield = get_field_object($field_key,$post_id);
    				// now get the label and value and whatever else you need from $field;
    				$message =  $message . ' <strong>- ' . $subfield['label'] . '</strong> : '. $subfield['value'] .'<br>';  
    			}
    		}
    	}	
    		
    	$message = $message; 
    		
    	wp_mail( $to, $subject, $message );	
    }
  • Actually, I’m still struggling… I managed to get the label easily, but I still cant get the value.

    $subfield[‘value’] doesn’t seem to work.
    I also tried get_field($subfield[‘name’]) but it doesn’t work either.

    I’m simply trying to receive an email with all the field labels and values when a user submits a form.

    Here is my full function :

    add_filter('acf/save_post', 'create_post_from_form');  
    
    function create_post_from_form($post_id) {	
    
    	$to = '[email protected]';
    			
    	$subject = 'test notification 8';
    	$message = '';
    		
    	$fields = get_field_objects($post_id);
    		
    	foreach($fields as $field) {
    			
    		$message =  $message . ' <strong>' . $field['label'] . '</strong><br>';	
    		$key = $field['key'];
    			
    		while (have_rows($key, $post_id)) {
    			// always happens once on a group field
    			$the_row = the_row(); // the row returns the row
    			// $the_row will be an array of "field_key" => "value" pairs
    			foreach ($the_row as $field_key => $value) {
    				$subfield = get_field_object($field_key,$post_id);
    				// now get the label and value and whatever else you need from $field;
    				$message =  $message . ' <strong>- ' . $subfield['label'] . '</strong> : '. $subfield['value'] .'<br>';  
    			}
    		}
    	}	
    		
    	$message = $message; 
    		
    	wp_mail( $to, $subject, $message );	
    }
  • Well… I’m still struggling…
    I easily managed to get the label : $field[‘label’]
    It’s a progress, but I still can’t get the value.

    My goal is to receive an email with all the field labels + values when a form is submitted. It should be a common request but I couldn’t find anything about it.

    Here’s my full code :

    
    add_filter('acf/save_post', 'create_post_from_form');  
    
    function create_post_from_form($post_id) {
    
    	$to = '[email protected]';		
    	$subject = 'test notification 8';
    	$message = '';
    		
    	$fields = get_field_objects($post_id);
    
    	foreach($fields as $field) {
    			
    		$message =  $message . ' <strong>' . $field['label'] . '</strong><br>';
    			
    		$key = $field['key'];
    			
    		while (have_rows($key, $post_id)) {
    		// always happens once on a group field
    		$the_row = the_row(); // the row returns the row
    		// $the_row will be an array of "field_key" => "value" pairs
    		foreach ($the_row as $field_key => $value) {
    			$subfield = get_field_object($field_key,$post_id);
    			$value = get_field($subfield['name'],$post_id);
    			// now get the label and value and whatever else you need from $field;
    			$message =  $message . ' <strong>- ' . $subfield['label'] . '</strong> : '. $subfield['value'] .'<br>';  
    		}
    	}
    }	
    	
    
    $message = $message;
    wp_mail( $to, $subject, $message );	
    
    }

    The first foreach allows me to get the labels of the several acf-field-group. And thanks to you I managed to get the labels of the subfields that these groups contain. But not the values.

  • Perfect, thanks! I had been struggling a lot on this one.

  • Hi,

    This works, but how can i get all the sub_fields in the group?
    In other words : I have a group of fields, and for all the fields in the group i want to display label and value (without having to specify their key or slug individually)

  • OK, I managed to do it with a acf/save_post function.

    add_filter('acf/save_post', 'create_proposal_from_form');  
    
    function create_proposal_from_form($post_id) {
    	
    	$test = get_field('test',$post_id);
    	$pagetype = is_page_template();
    	
    	if ($pagetype = 'Proposal Form') {
    		
    		$post_data = array(
    				'post_title' => 'Test7', 
    				'post_status' => 'publish',
    				'post_name' => 'test1',
    				'post_type' => 'enquiry',
    			);
    
    		$newpageid = wp_insert_post( $post_data );	
    		update_field('form_event_nights',$test, $newpageid);	
    	}
    }
  • Well I eventually decided for the custom post type option.
    I don’t think it’s possible to delete a thread ?

  • OK so i found the problem. Somehow, being an options page, i have to target the page :

    delete_row('myrepeater', $index, 'my-options-page');

  • Nevermind, I managed to sort my table with Jquery instead, which is just as fine.
    Thank you

  • Hi John,
    So if i understand you correctly, what I want to do is not possible ?

  • Hi,

    Indeed i’m not trying to sort posts.

    I had seen the link you posted, but if I understand correctly this is used to sort a repeater in one post ? Like, if i’m in the post “John”, i can sort the payments of that client by date.

    Would this also allow me to get all the payments(repeater rows) from all the clients(posts) and sort them by date?
    If yes, then I’ll look more closely into it or ask my dev.

    Thank you !

  • Hi John,

    Ok, thanks. Indeed, the easiest will probably be to simple add a new field and “alter the template file to look for a value in the new field. If no value is found then look for a value the old field”.
    I wanted to know if there was a cleaner way to do it, but let’s go for the safest/simplest.

    I suppose i can also remove the old field manually, as it won’t delete the values anyway?

    Thank you !

  • Thank you very much !
    Hmm, I wouldn’t know how to proceed using a repeater, I’d have to look into it. What would be the advantage compared to a simple message field with a php query?

  • Hi John,

    One question; while the function works fine, it creates 2 post revisions on each save. I don’t really understand filters quite yet so i didn’t manage to fix the issue myself…

  • Hi John,

    Thank you very much, worked fine.

    Have a good day.

  • Hahah indeed…
    Thank you very much, it works perfectly !

  • Hi John,

    Thank you for the quick reply.
    I tested your code and it generates a critical error.
    I checked for basic syntax errors and tried different things, but this goes beyong my php skills…

  • Hi John,

    Just to follow up on this subject, I tested a solution to the issue you raised in your november 2nd post.

    The issue: hiding fields by conditional logic in a repeater is not a good idea because when reordering rows, hidden fields will not be reordered and everything will be mixed up ​

    My solution: It is pretty hacky, but it seems to work fine.

    1. I hid the reorder column of my repeater via CSS

    2. I created a .reordermode class that, when applied to the repeater :
    – Makes the reorder column available again
    – Hides most fields of the repeater with display:none

    3. I created a true/false field that, when changed to TRUE:
    – removes via JS all the .acf-hidden, .acf-empty classes, hidden attribute from all divs of the repeater, as well as the disabled attribute from all inputs.
    – adds the class .reordermode to the repeater via JS

    => I can reorder the repeater knowing that all values will be saved.

    4. Also, when clicking on TRUE, a class is added to the true/false so as it is not possible to click back to FALSE before saving, (it also adds a message to remind users to save)

    5. When saving the post the class is not saved so the button is available and i can click back to FALSE.*
    Also, the “.acf-hidden, .acf-empty, hidden, disabled” classes and attributes are only removed on Change, not on load, so when I save the post they are all back on.

    $("#acf-field_61a74bcfc88d5").on('change', function() {
       ​if ($(this).is(':checked')) {
           ​$("#myrepeater").addClass("reordermode");
    		$("#myrepeater div").removeClass("acf-hidden");
    		$("#myrepeater div").removeClass("acf-empty");
    		$("#myrepeater div").removeAttr("hidden");
    		$("#myrepeater input").removeAttr("disabled");
    		$("#myrepeater select").removeAttr("disabled");
    		$("#myrepeater textarea").removeAttr("disabled");		
       ​}
       ​else {
          ​$("#myrepeater").removeClass("reordermode");
       ​}
    });

    * It would be even better if the true/false value was turned back to false automatically after saving, but i don’t know how to do that

  • Hi John,

    Thank you for your reply. Hmm, what i take from your answer is that it is a bit risky to proceed this way. It’s okay, i can add the conditional rules individually, I just wanted to make it safer/more efficient.

  • Hi @hube2

    Sorry, following your last comments I thought of another solution.

    So I have many, many subfields in my repeater. Most of these fields are “secondary” and belong to a group that is hidden via conditional logic.
    When I want to check/edit these secondary fields, I click on a true/false located outside of the repeater.

    When I’m done editing, I will usually want to hide them again via the true/false to keep only the essential information. But of course if i forget to save the post before hiding the fields, they won’t be updated.

    I thought of a solution that is clearly a hack :
    – When clicking on the true/false to TRUE, it adds a css class that hides it (so it can’t be clicked back to FALSE)
    – When done editing these secondary fields, the user saves the post. Via a custom function, the true/false value is returned to FALSE after save.

    Is it a bad idea ?

    As for your comment about reordering rows of a repeater when some fields are hidden by conditional logic I thought of something similar :
    – I hide the reordering column via CSS
    – I add a true/false, that when clicked to TRUE :
    *Becomes hidden via CSS
    *Displays the reordering column vis CSS
    *Displays all the hidden fields via conditional logic
    – I reorder the rows. Again I can’t click back to FALSE, So I save the post, which would put the true/false back to FALSE.

    By doing this I solve the performance issues (secondary fields would only be updated in the DB when needed) and the issues caused by repeater and conditional logic that you mentioned.

  • Damn. This is not good news and makes me have to reconsider many things that I had already built…

    Question: If, instead of hiding fields via conditional logic I hide them via CSS and JS (“if value of true/false is not 1 => add css class to this group of fields”) would it cause the same problem that you describe ?
    I suppose not but I want to make sure before going further.

Viewing 25 posts - 1 through 25 (of 28 total)