Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hassanshahzadaheer's avatar

integrate PHP code with contact form 7 fields

Hi! the question from WordPress but if anyone knows and helps me I will very thankful to you

question description in this link

https://stackoverflow.com/questions/56652051/integrate-php-code-with-contact-form-7-fields

0 likes
3 replies
Braunson's avatar

You'll want to look into the Hooks the Contact Form 7 provides. You'll hook into the response to output what you want.

As for the dropdowns, you create them in the Contact Form 7 form editor with the provided options.

You can see a list of hooks at http://hookr.io/plugins/contact-form-7/5.1.3/hooks/#index=a (for the latest version of CF7), you'll probably want something like the wpcf7_ajax_json_echo hook to modify the outbound response returned to the user. Here's an example:

Something like this (untested), but it would go in your wp-content/themes/footheme/functions.php (your theme, functions file).

add_filter('wpcf7_ajax_json_echo', function( $response, $result )
{
    $cow  = 100;
    $goat = 200;

    if (isset($response['cow']) || isset($response['goat'])) {
        if (isset($response['cow'])) {
            $cowSelect = $response['cow'];
            $cow = ($cow - $response['cow']);

            switch ($cowSelect) {
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                    $message .= 'Total '.$cow.' cows remaning'.'<br/>';
                    break;
            }
        }

        if (isset($response['goat'])) {
            $goatSelect = $response['goat'];
            $goat = ($goat - $response['goat']);

            switch ($goatSelect) {
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                    $message .= 'Total '.$goat.' goats remaning'.'<br/>';
                    break;
            }
        }
    }

    return $response;
}, 10, 2);

Then you just need to create a dropdown with the options you want, make the dropdown required (if it needs to be required) in the Contact Form 7 form editor.

hassanshahzadaheer's avatar

Hi! dear @braunson thank you so much for grate answer. Can you please help me how to add dropdown and display in my form because I am very stuck with this problem.

thank you so much

Please or to participate in this conversation.