0

I am new to wordpress but not to php programming. Yet I know very little of Javascript.

I am using wordpress 6.4.1 with theme twentytwentythree.

I have a form in ContactForm7 and I want to use it as a regular form, as in

form action="ww.php" method="post"

ww.php will collect the data and form a string that will be passed to WhatsappWeb using curl. This is because the people who will process the data want to get it by Whatsapp, not by mail

I did my searches. I modified functions.php in my theme this way:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url)
{
    global $post;
    $id_to_change = 1857;
    if($post->ID === $id_to_change)
        return 'https://[domain]/ww.php';
    else
        return $url;
}

apply_filters( 'wpcf7_load_js', '__return_false' );

it gives "form action="https://[domain]/ww.php" method="post">" but it does nothing, it just sends the mail

Then I did this in functions php

function ti_custom_javascript() {
    ?>
        <script>
          document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '1856' == event.detail.contactFormId ) {
    setTimeout( () => {
        location = 'https://[domain]/ww.php/';
    }, 3000 ); // Wait for 3 seconds to redirect.
    }
}, false );
        </script>
    <?php
}
add_action('wp_head', 'ti_custom_javascript');

(post ID is 1857, form ID is 1856)

This redirects the page but POST data is lost

I have read ways to keep POST data but I don't understand where to use them.

Thanks and forgive my bad English.

4
  • WordPress uses hooks and so does ContactForm7. What you're doing feels kinda "hacky". I'd suggest disabling the email notification in the settings and use the wpcf7_posted_data hook to retrieve data and submit it to Whatsapp. Commented Mar 22, 2024 at 15:49
  • Thank you, can you elaborate you answer? Where do I use the hook? Commented Mar 22, 2024 at 16:03
  • I tested your function wpcf7_custom_form_action_url and the POST data was passed to my new form action URL (when wpcf7_load_js is disabled like you have). At the same time... You can use the wpcf7_before_send_mail hook to pass data to a 3rd party API using wp_remote_post or wp_remote_get rather than curl Commented Mar 23, 2024 at 1:38
  • Thank you Howard. I solved it! I had to replace "apply_filters" with "add_filter" Commented Mar 23, 2024 at 20:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.