feat(wpforms): add single/double opt-in setting to WPForms integration#823
Open
faisalahammad wants to merge 1 commit intoibericode:mainfrom
Open
feat(wpforms): add single/double opt-in setting to WPForms integration#823faisalahammad wants to merge 1 commit intoibericode:mainfrom
faisalahammad wants to merge 1 commit intoibericode:mainfrom
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 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.