Custom contact form 7 select with custom values [closed]

You can set the values with the filter wpcf7_form_tag and add the shortcode attributes with the filter shortcode_atts_wpcf7

add_filter("wpcf7_form_tag", function($scanned_tag, $replace){


    if ("press_post" === $scanned_tag["name"]) {

        $contact_form = WPCF7_ContactForm::get_current();

        $number_of_posts = $contact_form->shortcode_attr("number_of_posts");
        $post_type = $contact_form->shortcode_attr("post_type");


        // using $number_of_posts and $post_type here


        $scanned_tag['raw_values'] = [
            "number_of_posts \"$number_of_posts\" - post_type \"$post_type\" | val 1",
            "Test 2|val 2",
            "Test 3|val 3",
        ];

        $pipes = new WPCF7_Pipes($scanned_tag['raw_values']);

        $scanned_tag['values'] = $pipes->collect_befores();
        $scanned_tag['pipes'] = $pipes;

    }


    return $scanned_tag;

}, 10, 2);


add_filter("shortcode_atts_wpcf7", function ($out, $pairs, $atts, $shortcode) {

    foreach (["number_of_posts", "post_type"] as $a) {

        if (isset($atts[$a])) {
            $out[$a] = $atts[$a];
        }

    }

    return $out;

}, 10, 4);

there is always “Test 2” in the HTML code but CF7 replace the value with “val 2” in the e-mail.

Leave a Comment