jkean99
jkean99

Reputation: 15

Return 0 or 1 on submit depending on whether a checkbox is checked - Contact Form 7

I"m trying to change how contact form 7 sends the value of a checkbox. I'm If the checkbox has been checked, then the checkbox value is 1 else the checkbox value equals 0;

I've tried the code below which just returns 1 regardless of whether the checkbox has been checked or not.

function action_wpcf7_posted_data( $array ) {     
    if ($array['optinsms'] == "" ) {
        $array['optinsms'] = 0;
    } else {
        $array['optinsms'] = 1;
    }
    return $array;
}

add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1);

Any help would be greatly appreciated!

Cheers,

Jasper

Upvotes: 0

Views: 220

Answers (1)

Rakib
Rakib

Reputation: 136

You are quite close. I believe the issue here is, checkbox response will always be an array. so you need to check the first index-

function action_wpcf7_posted_data( $array ) {     
    if ($array['optinsms'][0] == "" ) {
        $array['optinsms'][0] = 0;
    } else {
        $array['optinsms'][0] = 1;
    }
    return $array;
}
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1)

This should work.

Thank you.

Upvotes: 1

Related Questions