'How to remove %20 from Contact Form 7 field
My wordpress website capturing url UTM from Google Ads via hidden field of Contact Form 7. But Contact Form 7 capturing keywords with %20 instead of space (Ex. life%20insurance%20policy) and sending to assigned email addresses.
How can I replace %20 with space?
Solution 1:[1]
You should make it url decoded value, so if its value saved in hidden input with %20 you’d better to do it via js like this:
var form = document.getElementsByClassName('wpcf7-form')[0];
form.addEventListener('submit', function(evt) {
form.elements['google_adv'].value = decodeURIComponent(form.elements['google_adv'].value);
}, { capture: true });
But if the changing process is in backend side you may try with php codes instead:
add_action( 'wpcf7_before_send_mail', 'some_function_name', 1 );
function some_function_name( $contact_form ) {
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = array();
$data['posted_data'] = $submission->get_posted_data();
$google = $data['posted_data']['google_adv']; // just enter the field name here
$mail = $wpcf7->prop('mail');
if($google !=''){
$mail['body'] = str_replace($google, urldecode($google), $mail['body']);
$mail['body'] = str_replace('[google_adv]', urldecode($google), $mail['body']);
}
$wpcf7->set_properties(array(
"mail" => $mail
));
return $wpcf7;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
