bigdaveygeorge
bigdaveygeorge

Reputation: 999

Change checkbox option value to 1 or 0 in Contact Form 7

I am using Contact Form 7 in WordPress and have the following field:

<div class="checkbox">[checkbox subscribed default:1 "Send me occasional email updates"]</div>

"Send me occasional email updates" becomes the input label and the value when this option is selected is "Send me occasional email updates".

I want the value to be 0 or 1.

I cannot seem to find a way to do this.

There is an "acceptance" field you can use that does this but it is more for terms & conditions acceptance as it requires the checkbox to be ticked - whereas I am giving the option to opt in/out so I cannot use that.

Upvotes: 4

Views: 19845

Answers (3)

Iggy
Iggy

Reputation: 2131

You can use such shortcodes in the form for checkbox to send value="1":

[checkbox optout use_label_element "Opt-Out of Insights and Communications|1"]

Then to show original value in the mail message you'll need to use:

[_raw_optout]

Works ofr me with latest wp and cf7

Upvotes: 1

Nathan Pond
Nathan Pond

Reputation: 336

I have a very hacky solution. We had the same issue with a subscription checkbox, the entire label was passed through to the email and the user requested a simple yes/no. I used a combination of a hidden field and jQuery to accomplish this.

First, add an additional checkbox with values "yes" and "no" (or whatever you want). Put it in a container that's hidden on your form:

<label class="checkbox-inline">
                        [checkbox updates id:updates "Please check here to receive updates ...."]
                    </label>

<div class="checkbox-inline hidden hidden-contact-check">
[checkbox updateshidden default:2 id:updateshidden "yes" "no"]
</div>

Then a bit of javascript that will keep the appropriate checkbox checked in the hidden field:

$(document).ready(function() {
    $('#updates.wpcf7-checkbox input').click(function() {

        $('#updateshidden.wpcf7-checkbox input[value=yes]').prop('checked', $(this).prop('checked'));
        $('#updateshidden.wpcf7-checkbox input[value=no]').prop('checked', $(this).prop('checked') === false);

    });
});

Then finally in the Mail tab for CF7 you use the field [updateshidden]

Subscribe: [updateshidden]

This will pass through a yes/no to the email rather than the label you provided.

Upvotes: 0

yash Wanth Shetty
yash Wanth Shetty

Reputation: 51

Not sure about the syntax of contact form 7 of WordPress, but usually this works:

<input id='testName' type='checkbox' checked = 'checked' value='Yes' name='testName'>Send me occacional updates

Let me know if it does not suit your purpose.

Upvotes: -1

Related Questions