Utilize the 'on send success' action hook to trigger dual actions in Contact Form 7 within WordPress

Is it possible to utilize the on sent ok action hook for two different purposes? I want to both track the email address and redirect the user to a thank you page. My attempts to do this in the 'Additional Settings' section of the Contact Form 7 panel have yielded varying results, depending on which form I apply it to.

on_sent_ok: "fnTransaction('Contacted', 'userid=' + [your-email]);"

on_sent_ok: "location.replace('http://xxxxx.com/thank-you');"

Can the action hook be used multiple times or is there a way to combine these actions into one line of code? Any assistance would be greatly appreciated!

Answer №1

Is it possible to simply integrate

location.replace('http://xxxxx.com/thank-you');
within the fnTransaction()-function?

Note:

Create a new function that merges both actions:

on_sent_ok: "mySentOkFunction('Contacted', 'userid=' + [your-email]);"

function mySentOkFunction(parameter1, parameter2){
    fnTransaction(parameter1, parameter2);
    location.replace('http://xxxxx.com/thank-you');
}

Answer №2

I'm not familiar with Contact Form 7, but maybe you could attempt the following:

on_sent_ok: "function(){ fnTransaction('Contacted', 'userid=' + [your-email]);location.replace('http://xxxxx.com/thank-you');}"

Answer №3

To implement the desired functionality, you should try using the following code snippets:

on_sent_ok: "trackTransaction('Submitted', 'user=' + [your-email]); window.location.href = 'http://xxxxx.com/thank-you';"

If the original method of using location.replace is not working for you, consider switching to a more direct approach:

window.location.href = 'http://xxxxx.com/thank-you';

To summarize, combine these changes into your final code snippet:

on_sent_ok: "trackTransaction('Submitted', 'user=' + [your-email]); window.location.href = 'http://xxxxx.com/thank-you';"

Answer №4

An effective method involves utilizing the hook wpcf7_contact_form_properties within a custom plugin. Below is an example of the plugin:

/*
Plugin Name: Implement Multiple WPCF7's on_sent_ok
Plugin URI: http://kadimi.com/wpcf7-javascript-programmatically
Description: Enable multiple uses of WPCF7's on_sent_ok.
Author: Nabil Kadimi
Version: 1.0
Author URI: http://kadimi.com/
*/

function update_wpcf7_settings( $settings, $contact_form_obj, $unused ){
    // Add custom settings for on_sent_ok
    $settings[ 'additional_settings' ] .= 
        "\n"
        . 'on_sent_ok: "console.log(1);"' . "\n"
        . 'on_sent_ok: "console.log(2);"' . "\n"
        . 'on_sent_ok: "console.log(3);"' . "\n"
    ;
    return $settings;
}
add_filter( 'wpcf7_contact_form_properties', 'update_wpcf7_settings' , 10, 2 );

In the provided plugin code, the on_sent_ok function is utilized three times.

You have the ability to specify which form is impacted by examining the $contact_form_object.

Source:

This code snippet is based on information from my blog post available here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Master the ins and outs of working with Angular and Webpack on

Encountering issues with the webpack-angular tutorial from egghead.io during the "ES6 with Babel" step. Requesting assistance in resolving the problem. Stuck at this specific step while following the tutorial: Error message in Chrome devTools: ........ ...

After updating Next.js, encountering ERR_REQUIRE_ESM and Package.json errors. Any solutions to resolve this issue?

After a significant hiatus from working with Node and Next.js, I found myself in a situation where most of my knowledge had vanished. Despite knowing that updating my packages could potentially cause issues, I decided to proceed anyway. The update took my ...

Sending information to a VueJS component

Hey there! I have a challenge in VueJS where I need to transfer Firebase Authentication user data (JSON) from the App.vue component to another component named Details.vue. The goal is to display the name of the logged-in user on Details.vue. In App.vue: ...

What is the best way to duplicate/rename/replace an image in PHP while utilizing form submission and radio buttons?

I am new to PHP and I am working on a feature that allows a user to change an image using radio buttons and form submission. The goal is to copy and rename one of two image files (Open/Closed.jpg) to another location (images/status/Status.jpg) without requ ...

Turn off automatic spell check suggestions

Currently, I have a pre element with read-only text and spell-checking enabled. However, when I right-click on a misspelled word, the auto-suggestion feature allows me to change the value in the pre element. Is there a way to disable this auto-correction f ...

The context environment is failing to update the current state

Working with context in React can be tricky for some, including myself. I was hoping the new Context API would make things easier, but I'm still facing some issues. While I can get the initial value to display, the updates based on my Effect are not r ...

the ajax method is not being executed

My specific requirement is to retrieve values returned from my aspx page in an HTML page. Thank you in advance for your assistance. <script type="text/javascript"> function getCars() { alert("Hello"); $.ajax({ type: "POST", ...

Designing Checkbox features with Bootstrap Glyphicons

I have successfully created a set of bootstrap icons functioning as radio buttons. Now, I am trying to achieve a similar effect with checkboxes, allowing users to select and store multiple options. I am struggling to figure out the best approach to impleme ...

Is there a way to still access the data from a radio button even if it hasn't been selected?

I'm currently working on a questionnaire feature and facing an issue where I need to capture all answers, even if the radio button is not checked. Here's a snippet of the HTML code: $('input:radio').each(function () { if ($(this). ...

What is the process for sending JavaScript with an Ajax request?

Working with ajax and javascript for the first time, I'm no expert in web development. Here is the code I've written and tested so far. I have a select div containing some options. <select id="month" onchange="refreshGraph()"> When an op ...

Link Quick Techniques

I currently have an Express server set up with CRUD methods My goal is to automatically trigger the get method whenever a post, put, or delete request is made. This will ensure that the view on the Front-end side gets updated accordingly. Below is an exc ...

I would like to fade out every element except for the one with the class "this

I need a solution where every div with the class "insegnanti" fades out, except for the one that I clicked on. Here is the script: for(var i=0;i<instructor.length;i++){ $(document).on ("click", ".insegnanti#i"+[i+1], loadSingleIns); el+="<div cl ...

Alter the hue of a component upon being clicked

Seeking help to modify the color of my menu elements upon clicking on the "center" or "right" containers (reverting back when clicked again). Currently, the three lines in my menu are white, but I desire them to turn red when either of these containers is ...

Utilizing Kendo UI Jsonp in conjunction with a WordPress json plugin: A comprehensive

Issue at Hand: My goal is to modify a kendo UI data-binding example to work with my own jsonp request. Description of the Problem: Starting from a data-binding example here, I have created this jsfiddle that demonstrates the desired functionality. To a ...

Troubleshooting problems with AJAX function in the .each() loop

My webpage showcases a grid layout with 16 blocks arranged in 4 columns and 4 rows. As you scroll to the bottom of the page, an AJAX function dynamically loads another set of 16 blocks each time. To enhance user experience, I wanted to implement a smooth ...

Ways to retrieve and upload a blob from a file object

After receiving a file object via an HTML input on-change event in my component.html: onFileSelected(event) { this.selectedFile = event.target.files[0]; } Now, I need to send a POST request to upload the image to the database. The database only acc ...

How to set up scroll restoration for the Angular Standalone Router?

The Angular Router provides the option to restore scrolling functionality, with documentation on how to implement it when loading data. Is there a way to configure the standalone router to automatically scroll back to the top of the router outlet? A demo ...

PHP variable missing "+" sign at the end after post operation

I have encountered a unique problem that I cannot seem to find anywhere else. My issue lies in adding grades ending with a plus sign, such as B+, C+ or D+. I am able to add grades like A-, B-, C- and D- without any problem, but when it comes to adding a g ...

What makes this lambda function in TypeScript successfully execute without any errors?

I was under the impression that this code let x: (a: { b: number }) => void = (a: { b: number, c: string }) => { alert(a.c) }; x({ b: 123 }); would result in an error because the lambda function requires an additional property on the a argument, m ...

How can I stop the hover event on a series in Highcharts from triggering a hover event on the nearest point (marker)?

When hovering over the series line on a date-time chart, the default behavior is to trigger the hover event on the nearest point of the series. However, I only want to trigger the hover event on the markers (points), not the series itself. If I use stopP ...