feat(wpforms): add single/double opt-in setting to WPForms integration#823
Conversation
- Add 'Double opt-in?' select dropdown to the WPForms Mailchimp field builder - Read per-field mailchimp_double_optin setting during subscription - Restore original options after subscribing to avoid side effects - Add unit tests covering default, single opt-in, and options restoration Fixes ibericode#710
|
Hi @faisalahammad. Not related to this PR per se, but can you instruct your AI to be more concise? This is a lot of text and a lot of it is unnecessary verbose. I really only care about the goal of the PR, special considerations and potential breaking changes. The code will speak for itself. Workflow ensures that tests pass and code style is consistent, no need for the AI to repeat that. |
|
Merged with a few tiny modifications! Thanks @faisalahammad. We'll be including all of these changes in the next plugin update, due next week. We'd like to give credits to you in the changelog, would you like us to link somewhere specific? |
|
If possible, please feel free to give credit to my personal portfolio website: https://faisalahammad.com. It would also be wonderful if you could include my WordPress.org username (faisalahammad) in the plugin contributor list, but no pressure at all, only if it’s convenient for you. I’ve really enjoyed working on your plugin and am glad to contribute through those PRs to help users. When I was at Ninja Forms last year, I noticed many of our users relied on your plugin. Since the default MailChimp checkbox wasn’t working well, we recommended using Conditional Logic as a solution. Thanks so much for creating such a great plugin! @dannyvankooten |
🎯 Summary
Adds a per-field "Double opt-in?" setting to the WPForms Mailchimp field, allowing administrators to toggle between single and double opt-in on a per-form basis — matching the existing Gravity Forms integration pattern.
📋 Issue Reference
Fixes #710
🔍 Problem Description
Current Behavior
The WPForms integration always uses double opt-in when subscribing users to Mailchimp. There is no way for site administrators to switch to single opt-in, forcing all subscribers to go through the confirmation email flow regardless of the use case.
Expected Behavior
Administrators should be able to choose between single opt-in and double opt-in on a per-form (per-field) basis, similar to how the Gravity Forms and Ninja Forms integrations already work.
Root Cause
The WPForms integration's
subscribe_from_wpforms()method directly calls$this->subscribe()without ever modifying$this->options['double_optin'], which defaults to1(enabled) in the baseMC4WP_Integrationclass. Additionally, the WPForms field builder UI (MC4WP_WPForms_Field) does not expose any setting for double opt-in, so the value is never stored in the form data.✨ Solution Overview
Approach Taken
Per-field setting in the WPForms builder — following the same pattern as the existing Gravity Forms integration:
<select>dropdown to the WPForms Mailchimp field's builder optionsmailchimp_double_optinvalue during form submission$this->options['double_optin']before callingsubscribe()Why This Approach
integrations/gravity-forms/class-gravity-forms.php), so the codebase stays uniform'1'(double opt-in enabled), so existing forms continue working identically without any migrationAlternatives Considered
'double_optin'toget_ui_elements(). Rejected because WPForms stores list selection per-field (not globally), so double opt-in should follow the same pattern for consistency. A global setting also wouldn't support different opt-in modes across different forms.mc4wp_integration_wpforms_double_optinfilter hook. Rejected because it requires custom code from the user — the whole point of issue WP Forms integration single / double opt in setting #710 is providing a UI setting that non-developers can use.🔧 Changes Made
Files Modified
integrations/wpforms/class-field.phpintegrations/wpforms/class-wpforms.phpmailchimp_double_optinsetting and apply before subscribingtests/WPFormsDoubleOptinTest.phpDetailed Changes
1. WPForms Field Builder UI —
class-field.phpAdded a new
field_option_double_optin()method and wired it into the field options panel:Why This Works:
field_element()API as the existing list selector and checkbox options, ensuring consistent rendering$field['mailchimp_double_optin']which is saved as part of the WPForms form data'1'(Yes) so existing forms are unaffected2. Integration Logic —
class-wpforms.phpBefore:
After:
Why This Works:
MC4WP_Integration::subscribe()reads$this->options['double_optin']to determine subscriber status ('pending'vs'subscribed'). By setting this value before callingsubscribe(), we control the opt-in behavior.$this->optionsafterward prevents the per-field override from leaking into subsequent form processing — this is the same defensive pattern used by the Gravity Forms integration.'1'whenmailchimp_double_optinis not set, ensuring backward compatibility with existing forms.Impact:
🧪 Testing Performed
Automated Testing
$ vendor/bin/phpunit Welcome to the Mailchimp for WordPress Test Suite PHPUnit 9.6.34 by Sebastian Bergmann and contributors. ........................................................... 59 / 59 (100%) Time: 00:00.024, Memory: 6.00 MB OK (59 tests, 185 assertions)New Tests Created —
tests/WPFormsDoubleOptinTest.php(5 tests, 10 assertions):test_default_double_optin'1'when not settest_single_optindouble_optinis set to'0'when configuredtest_options_restored_after_subscribetest_listen_to_wpforms_triggers_subscriptiontest_listen_to_wpforms_skips_uncheckedRegression Testing
📊 Performance Impact
✅ Negligible — The only addition is reading one extra field from the already-loaded
$form_dataarray and one extra variable assignment. No additional database queries, API calls, or file I/O.🔒 Security Considerations
mailchimp_double_optinvalue is read from$form_data['fields']which is already sanitized by WPForms during form save. It's used as a truthy/falsy value in the base class (? 'pending' : 'subscribed'), not interpolated into SQL or HTML.<select>dropdown in the builder usesselected()(WordPress core function) for safe attribute output.🌍 Internationalization
__()with text domainmailchimp-for-wp✅ No breaking changes — Fully backward compatible. The
mailchimp_double_optinfield defaults to'1'when not present, maintaining identical behavior for all existing WPForms forms.Screenshot / Screen recording
https://cln.sh/ylJ1Lk35
✅ PR Checklist
🤝 Additional Context
integrations/gravity-forms/) was used as the primary reference for this implementation, as it already supports per-field double opt-in in the same way.