Using Gravity Forms ‘gform_mailchimp_allow_resubscription’ PHP filter

The gform_mailchimp_allow_resubscription is a Gravity Forms PHP filter that allows you to modify whether a user with an unsubscribed status on your list should be resubscribed or not. By default, users are resubscribed.

Usage

To prevent users from being resubscribed, you can use the following code:

add_filter("gform_mailchimp_allow_resubscription", "__return_false");

To apply this filter to a specific form, add the form ID after the hook name:

add_filter("gform_mailchimp_allow_resubscription_1", "__return_false");

Parameters

There are no parameters for this filter.

More information

See Gravity Forms Docs: gform_mailchimp_allow_resubscription

Examples

Prevent all users from being resubscribed

To prevent all users from being resubscribed, use the following code:

add_filter("gform_mailchimp_allow_resubscription", "__return_false");

Prevent users from being resubscribed for a specific form

To prevent users from being resubscribed only for form with ID 1, use the following code:

add_filter("gform_mailchimp_allow_resubscription_1", "__return_false");

Allow resubscription based on a custom condition

To allow resubscription based on a custom condition, create a function and return true or false based on your condition. In this example, we only resubscribe users with a specific email domain:

add_filter("gform_mailchimp_allow_resubscription", "custom_resubscribe_condition", 10, 4);

function custom_resubscribe_condition($allow_resubscription, $email_address, $list_id, $form_id) {
    if (strpos($email_address, "@example.com") !== false) {
        return true;
    } else {
        return false;
    }
}

Conditionally prevent resubscription for a specific form

To prevent resubscription for users with a specific email domain only for form with ID 1, use the following code:

add_filter("gform_mailchimp_allow_resubscription_1", "custom_resubscribe_condition_form_1", 10, 3);

function custom_resubscribe_condition_form_1($allow_resubscription, $email_address, $list_id) {
    if (strpos($email_address, "@example.com") !== false) {
        return false;
    } else {
        return true;
    }
}

Allow resubscription for users who have a specific custom field value

To allow resubscription only for users who have a specific custom field value, use the following code:

add_filter("gform_mailchimp_allow_resubscription", "custom_field_resubscribe_condition", 10, 4);

function custom_field_resubscribe_condition($allow_resubscription, $email_address, $list_id, $form_id) {
    // Replace 'CUSTOM_FIELD_NAME' with the actual custom field name
    $custom_field_value = get_user_meta(get_current_user_id(), 'CUSTOM_FIELD_NAME', true);
    if ($custom_field_value == 'desired_value') {
        return true;
    } else {
        return false;
    }
}

Note: This code should be placed in the functions.php file of your active theme. The filter is located in mailchimp.php.