Duplicate an Entry in Formidable forms
If your users are creating many form entries with similar field values in your Formidable Forms, they won’t like to type the same values again and again. In this instance, it is a good user experience to provide a duplicate entry button. Formidable doesn’t provide this out of the box. However, they have shared a custom code snippet on their website https://formidableforms.com/knowledgebase/php-examples/#kb-duplicate-an-entry
While this works, it is not a perfect solution. What this code snippet does is, upon clicking the “Duplicate” link, it creates a duplicate entry in the database and then shows the newly created entry in edit mode in the form. User can make changes in the new entry and save it again. The issue with this approach is it doesn’t support the case where the user clicks on the “Duplicate” link and then decided not to create the new entry. Since the new entry is created immediately upon clicking the link, it is too late to discard the form. The user will be confused with the new entry that unexpectedly showed up in their list.
There are more issues to this approach.
- If the user refresh the page for some reason, it will create more and more duplicate entries without their knowledge.
- The page for creating a brand new entry and the page for creating an entry by duplication can’t be the same.
There is a better solution. It is simple and solves all the problems mentioned above. All you have to do is
1. Add the following code snippet. Replace your form_id at line 5. Optionally add keys of any fields in your form that you don’t want to copy to new entry (line 6)
//Form will get its field values from entry id given in URL param 'copy_entry' add_filter('frm_get_default_value', 'fill_values_for_duplicate_entry', 10, 2); function fill_values_for_duplicate_entry( $new_value, $field ){ if(isset( $_GET['copy_entry']) && !in_array( $field->type, array("hidden", "divider", "html", "end_divider", "user_id" ))) { if ( $field->form_id == 2) { //Replace 2 by Form ID if(!in_array( $field->field_key, array( 'xxxx', 'xxxx', 'xxxx'))){ //Replace xxx with keys of fields to skip copying $new_value = set_field_default_value($field); } } } return $new_value; } function set_field_default_value($field) { if( $field->type == 'data') { return do_shortcode( '[frm-field-value field_id=' . $field->id . ' entry=copy_entry show=id]' ); } elseif( $field->type == 'textarea') { return do_shortcode( '[frm-field-value field_id=' . $field->id . ' entry=copy_entry wpautop=0]' ); } else { return do_shortcode( '[frm-field-value field_id=' . $field->id . ' entry=copy_entry]' ); } }
2. Create “Duplicate” link with the following format in your view where you show the list of entries. Here replace “my-form” with the path to the page where you have your form
<a href="/my-form?copy_entry=[id]">Duplicate</a>
The idea behind this solution is simple. What it does is, if the URL has “copy_entry” query param, it gets the original entry id from it then uses it to get the values of each field in the original entry and sets them as default values in the new form. New entry is not created until the user clicks on the save button. If the URL doesn’t have the “copy_entry” param, the form works as usual where the user has to enter everything.
Note: I have tested this with the commonly used Formidable forms field types. If it doesn’t work for any field types, please comment here, I will try to fix it. Alternatively, you can share if you have a fix for it.