Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save annuman97/94713380b6838f1435d20b2809e4799d to your computer and use it in GitHub Desktop.

Select an option

Save annuman97/94713380b6838f1435d20b2809e4799d to your computer and use it in GitHub Desktop.
Temporary Fluent Forms workaround to preserve plus-addressed email values in redirect query strings.
<?php
// Temporary Fluent Forms workaround:
// Preserve plus-addressed emails when passing email through redirect query string.
add_filter('fluentform/submission_confirmation', function ($returnData, $form, $confirmation, $insertId = null, $formData = []) {
$sourceFormId = 29; // Form #1 ID
$emailFieldName = 'email'; // Form #1 email field name
$queryKey = 'email'; // Redirect URL query key: ?email=
if ((int) $form->id !== $sourceFormId || empty($returnData['redirectUrl'])) {
return $returnData;
}
if (!is_array($formData) || empty($formData[$emailFieldName])) {
return $returnData;
}
$email = sanitize_email($formData[$emailFieldName]);
if (!$email || !is_email($email)) {
return $returnData;
}
$returnData['redirectUrl'] = wpmn_fix_plus_email_redirect_url(
$returnData['redirectUrl'],
$queryKey,
$email
);
return $returnData;
}, 10, 5);
if (!function_exists('wpmn_fix_plus_email_redirect_url')) {
function wpmn_fix_plus_email_redirect_url($url, $queryKey, $value)
{
$fragment = '';
if (strpos($url, '#') !== false) {
list($url, $fragment) = explode('#', $url, 2);
$fragment = '#' . $fragment;
}
$encodedKey = rawurlencode($queryKey);
$encodedValue = rawurlencode($value);
$pattern = '/([?&])' . preg_quote($queryKey, '/') . '=[^&#]*/';
if (preg_match($pattern, $url)) {
$url = preg_replace_callback($pattern, function ($matches) use ($encodedKey, $encodedValue) {
return $matches[1] . $encodedKey . '=' . $encodedValue;
}, $url, 1);
} else {
$separator = strpos($url, '?') === false ? '?' : '&';
$url .= $separator . $encodedKey . '=' . $encodedValue;
}
return $url . $fragment;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment