skip to Main Content

contact form 7 I want to run php code after submitting ? is it possible ?
I have php curl code I want to run it but I don’t know how to do it.

my php code :


<?php


$data = array(
    'chatId' => '[email protected]',
    'mentions' => array(
        '[email protected]'
    ),
    'text' => 'Hi there!',
    'session' => 'default'
);


$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => "Content-type: application/jsonrn",
        'content' => json_encode($data)
    )
);


$context = stream_context_create($options);
$result = file_get_contents('http://1.1.1.1:3000/api/sendText', false, $context);


echo $result;
?>

Is it possible how to do it?

2

Answers


  1. yes, it is possible. you’ll need to use a WordPress hook wpcf7_mail_sent to do it. You need to wrap your cURL code into a funtion and then use the hook as follow:

    add_action('wpcf7_mail_sent', 'your_function');
    
    Login or Signup to reply.
  2. Yes, it is possible to execute custom PHP code after a form submission with Contact Form 7 in WordPress. However, to integrate custom PHP code like yours, you need to hook into Contact Form 7’s wpcf7_before_send_mail action. This action allows you to run custom code before the email is sent, which is essentially right after the form submission.

    Here’s how you can do it:

    Add the following code to your theme’s functions.php file or a custom plugin. This code hooks into the wpcf7_before_send_mail action and executes your custom PHP code.

    add_action('wpcf7_before_send_mail', 'custom_action_after_form_submit' );
    
    function custom_action_after_form_submit($contact_form) {
        $title = $contact_form->title();
        $submission = WPCF7_Submission::get_instance();
    
        // Check if this is the form you want to trigger your action on
        // You can remove this if check if you want it to apply to all forms
        if ('Your Form Name' == $title) {
            // Your custom PHP code here
            $data = array(
                'chatId' => '[email protected]',
                'mentions' => array('[email protected]'),
                'text' => 'Hi there!',
                'session' => 'default'
            );
    
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => "Content-type: application/jsonrn",
                    'content' => json_encode($data)
                )
            );
    
            $context = stream_context_create($options);
            $result = file_get_contents('http://1.1.1.1:3000/api/sendText', false, $context);
    
            // Optionally, you can log the result or perform further actions based on it
            // error_log($result);
        }
    }
    

    Replace ‘Your Form Name’ with the actual name of your Contact Form 7 form. This is to ensure the custom code runs only for the specified form. If you want your code to run for every form submission, you can remove the if condition that checks the form title.

    Ensure that your server is configured to allow outgoing HTTP requests via file_get_contents and that it supports the PHP stream context options you’re using. Some hosting environments disable file_get_contents for URLs or restrict its usage due to security reasons. In such cases, you might need to use cURL or a WordPress-specific HTTP API like wp_remote_post.

    Be cautious with security. Make sure your target URL (‘http://1.1.1.1:3000/api/sendText’) is secure and trusted. Sending data to external servers can introduce security risks.

    After adding this code, test your form submission to ensure that your custom PHP code executes as expected and the external API call is made successfully.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search