Archived topic
code in footer.php
7 replies · Started by Matthew on June 7, 2019
I have been told to add below snippet to the footer.php, and doing so will enable CF7 plugin to not clear the form field values from all the fields in a form after its submitted.
<script>
document.addEventListener( ‘wpcf7mailsent’, function( event ) {
jQuery(‘.wpcf7’).find(‘form’)[0].reset();
}, false );
</script>
I added the above code as displayed into footer.php, but it's not working. Am I doing something wrong? Should it be put in a certain area of the php file?
I copied footer.php into my child theme folder first, then pasted the code into that.
Also tried it with php open and close tags (instead of 'script')
It seems weird to add this to the footer. Should it not be functions.php?
thanks,
Hi there,
try adding the code to a Hook Element:
https://docs.generatepress.com/article/hooks-element-overview/
You can select the WP_Footer hook. And then set your display rules for the entire site or just the pages where the form is.
Hi David,
thanks for the info about Elements. Learnt something new.
Did exactly as you said but not working though. Can only assume the CF7 core file code is still overriding it
wp-content\plugins\contact-form-7\includes\js\scripts.js
`if ( 'mail_sent' == data.status ) {
$form.each( function() {
this.reset();
} );`
It could be. Where did you get the other code from? Does the provider of that provide any explanation?
I have just sent a support request about it to them,
Let us know what they come back with
Ended up going with a direct edit to the plugin source as described here:
https://wordpress.org/support/topic/how-to-not-clear-or-autofill-certain-fields-after-submission/
not sure if this can be put in a 'child theme' for plugins. But it does work well. Radio and checkboxes clear the data so I use text fields and combo boxes only.
thanks
Glad to hear you found a solution.