Contact form 7 redirecting issue - wordpress

After submission, my contact form redirects to a URL followed by an unfamiliar code like #wpcf7-f1258-o1. but i used on_sent_ok: "my_redirect();" on additional heading.
Please help!

The on_sent_ok method has been removed from Contact Form 7.
See: https://contactform7.com/2017/06/07/on-sent-ok-is-deprecated/
Try using a hook on the page where the contact form is located:
add_action('wp_head', 'cf7_redirect_script');
function cf7_redirect_script(){
if(is_page('page-slug') { ?> // slug of the page which your contact form is on (can also be an ID)
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
// put the desired redirect URL below
location = 'http://yourdomain.com/thank-you';
}, false );
</script>
<?php }
}

Related

Why is Disabling recaptcha using wp hooks for specific page with contact form makes the contact form 7 sending email not working in WordPress?

I added this hook to disable recaptcha on homepage but my homepage has a contact form 7 and when I test the form it wont work anymore. Removing the hook that I added makes it work again. Is there another way to disable the recaptcha without affecting the contact form 7? HERE is the hook.
add_action('wp_print_scripts', function () {
if (is_page(array( 'home' )) ) {
wp_dequeue_script('wpcf7-recaptcha');
wp_dequeue_script('google-recaptcha');
}
});
add_action('wp_print_scripts', function () {
if (is_page(array( 'home' )) ) {
wp_dequeue_script('wpcf7-recaptcha');
wp_dequeue_script('google-recaptcha');
}
});

Contact Form 7 not submitting using Ajax

I have a form that's inserted into a template using shortcode
<?php echo do_shortcode('[contact-form-7 id="370" title="Contact form 1"]') ?>
I'm trying to setup an event so that when the form is submitted I can redirect the user to another page. I'm using the following;
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'http://example.com/';
}, false );
</script>
The problem is, when you submit the form, the whole page is reloaded so the event is never triggered because the form isn't submitted via an ajax call.
This is the first time I'm trying to integrate a form into Wordpress, so I think I may have missed something. There are no errors in the developer console in Chrome.
Somewhere in your theme (check functions.php) or your wp-config.php file, you need to look for and remove the following:
functions.php
add_filter( 'wpcf7_load_js', '__return_false' );
config.php
define( 'WPCF7_LOAD_JS', false );
These lines would prevent the default behavior of CF7.
My theme was missing;
Adding that solved the issue.

CF7 hook breaks form after submit

I'm trying to use some CF7 hooks, but they seem to break something in the workflow after the submit.
I tried for example to add the following snippet to print something in the console:
function debug_to_console($cf7) {
echo '<div display="none"><script type="text/javascript">console.log("console log message");</script></div>';
//return $cf7;
}
add_action( 'wpcf7_before_send_mail', 'debug_to_console' );
When I enable it, nothing gets printed in the console and the [response](the notification after submitting the form) stops working.
The email instead gets delivered.
Any idea?
Thanks!
Use javascript events for frontend. https://contactform7.com/dom-events/
If you want fire action before ajax call use:
$('.wpcf7-form').submit(function() {
// action
});
We can not echo the result to front end from the wordpress hooks.
Try DOM Events from Contact form 7
document.addEventListener( 'wpcf7submit', function( event ) {
var entry = event.detail.inputs.find(element => element.name == 'entry_id');
switch(event.detail.contactFormId){
case "220": case "222":
alert(event.detail);
break;
default : console.log("Error");break;
}

Send an Email and Download a PDF on click of Contact Form 7 Submit button

I have a contact form in my Wordpress website. In that I have three fields Name, Email and Mobile, and a button called Submit. When the user fills all the fields and click on Submit button an email should send which can possible with Contact From 7 plugin.
But challenge here is I need to make the user download a PDF also, when he clicks on Submit button upon filling all the fields.'
How can I achieve this in Wordpress?
You can use the wpcf7_mail_sent hook provided by Contact form 7 like this:
add_action('wpcf7_mail_sent', function ($cf) {
// Run code after the email has been sent
});
This link: https://contactform7.com/2017/06/07/on-sent-ok-is-deprecated/ also describes another way:
add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer()
{ ?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event )
{
//Write a javascript code to download the file.
} , false );
</script>
<?php
}
Since the on_sent_ok has been deprecated, here is an example you could use as inspiration.
I had the similar need to add a download CTA after the content of all case studies of a website, but "in exchange" of user's data for:
display a CF7 form on your page, I had the same one on all case studies post type single which I hooked after the content
find a way to get the wanted PDF url for people to download, as for me all case studies have a different PDF, I simply added an ACF field, filtered on PDF only, which returns the file url
based on CF7 Dom events, choose the action you prefer to make the dowload happens, as I am not sending any confirmation email, I prefer working on the wpcf7submit event. Note that wpcf7submit event is fired only if the form has been validated
So the code looks like this:
<?php
// For simplicity, using an anonymous functions
add_action( 'wp_print_footer_scripts', function () {
// Check the wanted singular post type loading
if ( is_admin() || ! is_singular( 'case-study' ) ) {
return;
}
// Check if the ACF PDF field is not empty for use
$pdf_link = get_field( 'pdf' );
if ( empty( $pdf_link ) ) {
return;
}
// Hook on the "wpcf7submit" CF7 Dom event to force the download
printf( "<script>document.addEventListener( 'wpcf7submit', function( event ) { window.open('%s'); }, false );</script>", $pdf_link );
} );

Contact Form 7 redirect after submission

I'm using contact form 7 and I'm trying to redirect to another page after a successful contact form submission.
I've tried using Contact Form 7 – Success Page Redirects (https://nl-be.wordpress.org/plugins/contact-form-7-success-page-redirects/) but the plugin isn't compatible with the theme and gives some errors.
Is there another way to redirect without using that plugin?
I've found this https://contactform7.com/redirecting-to-another-url-after-submissions/ too, but I'm not able to implement it. The redirection is also only necessary for one contact form on the site, not all of them.
Thank you!
J.
I've seen quite a few answers with the same responses. The major question comes when you have 10 forms and 10 different thank you pages and this solution won't work.
I have a workaround for this.
Step 1: Create a hidden field in your form and add the thank you page URL in that.
[hidden thankyouURL id:thankyouURL default:http://example.com/thank-you/ "http://example.com/thank-you/"]
Step 2: In the DOM event, get the thank you URL from the field and redirect the user.
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
var thankyouURL = document.getElementById("thankyouURL").value;
location = thankyouURL;
}, false );
</script>
That's it.
Add below code in functions.php (located in themes -> themeName Folder)
add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( e ) {
var str = window.location.href;
if( str.includes("flp") ){
window.location.href = "http://www.YourWebsite.com/facebook-thank-you";
} else if( str.includes("glp") ){
window.location.href = "http://www.YourWebsite.com/google-thank-you";
}
}, false );
</script>
<?php
}
I'm trying to do the same thing but yet no success. the on_sent_ok is about to be no longer recommended. Check this page DOM EVENTS on the end of the page you can find the code for a specific form.
What do you do if you have multiple forms on the same page? Using the current answers, this could be a problem (For example, IDs must be unique, or the redirect ID is on the page but the user submits a different form).
The code below attempts to fix these potential issues. This answer uses hidden form fields in CF7, but allows you to have a unique redirect URL for each form without having to edit your JS code every time you create a new form (just use a consistent name, such as "url_redirect" as shown in the code below):
Contact Form 7:
[hidden url_redirect "http://customurl.com?customvar=1"]
Javascript:
document.addEventListener( 'wpcf7mailsent', function( e ) {
var url_redirect = '';
var inputs = e.detail.inputs;
for ( var i = 0; i < inputs.length; i++ ) {
if( 'url_redirect' == inputs[i].name ) {//used for misc forms
url_redirect = inputs[i].value;//set the redirect value from current submitted form
}
}
//Check for redirect
if( url_redirect ){
location = url_redirect;
}
}, false );
****EASY SOLUTION****
The options of using the EventListener script didn't work for me, but I found a super simple solution. Just add the Wordpress plugin called "Redirection for Contact Form 7". (Look at screenshot below).
After installing the plugin, a new tab called "Redirect Settings" will appear when you go inside any of your created Contact Forms ( Look at the 2nd screenshot). Here you have the option of either setting one of your already existing custom pages of the project as redirection url, or setting an external url for the purpose. You also have other options like setting a delay in redirection etc.

Resources