• It’s been requested before to add cf turnstile to the recaptcha options, but while waiting for that to be added as a feature, I am trying to do it manually. Is it possible to validate a mailpoet form using a cf turnstile if added manually? I am trying the below in my functions.php file but I can’t get it to work. The only error in the mailpoet logs I get is this [2025-01-25T01:01:52.309477+00:00] mss.ERROR: key-validation.failed {“http_code”:403,”home_url”:”my-website”,”key_type”:”premium”} {“free_plugin_version”:”5.6.2″,”premium_plugin_version”:”premium not installed”}

    As far as I can see from the list of filters I’m not using any premium filters. On form submissions I only get “Turnstile verification is required.” or “You need to wait 24 hours before subscribing again.” and can’t get it to submit. The cf turnstile is working correctly and creating a valid token. Not sure if I’m close or way off or this isn’t possible. Any ideas?

    /**
    * @snippet Add Cloudflare Turnstile to <head>
    */

    function add_turnstile_script_to_head() {
    echo '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>';
    }
    add_action('wp_head', 'add_turnstile_script_to_head');


    /**
    * @snippet Insert Cloudflare Turnstile to Mailpoet forms
    */

    add_filter('mailpoet_form_widget_post_process', function($form_html) {
    $turnstile_html = '<div class="cf-turnstile" data-sitekey="my-site-key" data-theme="light"></div>';

    // Inject the Turnstile widget just before the closing </form> tag
    return str_replace('</form>', $turnstile_html . '</form>', $form_html);
    });


    /**
    * @snippet Validate Cloudflare Turnstile on Mailpoet Forms
    */

    add_action('mailpoet_subscription_before_subscribe', function($data, $segmentIds, $form) {
    // Check if Turnstile response is set
    if (!isset($_POST['cf-turnstile-response'])) {
    throw new \MailPoet\UnexpectedValueException(__('Turnstile verification is required.', 'text-domain'));
    }

    // Validate the Turnstile response
    $turnstile_response = $_POST['cf-turnstile-response'];
    $is_valid = validate_turnstile_response($turnstile_response);

    if (!$is_valid) {
    throw new \MailPoet\UnexpectedValueException(__('Turnstile verification failed. Please try again.', 'text-domain'));
    }
    }, 10, 3);

    // Function to validate the Turnstile response
    function validate_turnstile_response($response_token) {
    $secret_key = 'my-secret-key';
    $verify_url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';

    $response = wp_remote_post($verify_url, [
    'body' => [
    'secret' => $secret_key,
    'response' => $response_token,
    'remoteip' => $_SERVER['REMOTE_ADDR'] ?? ''
    ]
    ]);

    if (is_wp_error($response)) {
    error_log('Turnstile validation error: ' . $response->get_error_message());
    return false;
    }

    $response_body = json_decode(wp_remote_retrieve_body($response), true);
    return isset($response_body['success']) && $response_body['success'];
    }
Viewing 1 replies (of 1 total)
  • Plugin Support Ojoma a11n

    (@geraltrivia)

    Hello there @pesac,

    Thank you for reaching out to us.

    Currently, MailPoet does not offer built-in support for Cloudflare Turnstile. Integrating it manually requires custom development.

    While we appreciate your efforts in implementing this manually, please note that assistance with custom code falls outside our scope of support.

    If you need help implementing a custom solution, we recommend reaching out to a professional developer. A great resource for hiring experienced WordPress developers is Codeable (affiliate link), where you can find experts to assist with your customization needs.

    Cheers!

Viewing 1 replies (of 1 total)

The topic ‘Adding Cloudflare Turnstile to Form’ is closed to new replies.