namespace WPForms\Admin\Settings\Captcha;
use WPForms\Admin\Notice;
* Slug identifier for admin page view.
* Saved CAPTCHA settings.
* All available captcha types.
// Only load if we are actually on the settings page.
if ( ! wpforms_is_admin_page( 'settings' ) ) {
// Listen the previous reCAPTCHA page and safely redirect from it.
if ( wpforms_is_admin_page( 'settings', 'recaptcha' ) ) {
wp_safe_redirect( add_query_arg( 'view', self::VIEW, admin_url( 'admin.php?page=wpforms-settings' ) ) );
public function init_settings() {
$this->settings = wp_parse_args( wpforms_get_captcha_settings(), [ 'provider' => 'none' ] );
* Filter available captcha for the settings page.
* @param array $captcha Array where key is captcha name and value is captcha class instance.
* @param array $settings Array of settings.
$this->captchas = apply_filters(
'wpforms_admin_settings_captcha_page_init_settings_available_captcha',
'hcaptcha' => new HCaptcha(),
'recaptcha' => new ReCaptcha(),
'turnstile' => new Turnstile(),
foreach ( $this->captchas as $captcha ) {
public function hooks() {
add_filter( 'wpforms_settings_tabs', [ $this, 'register_settings_tabs' ], 5, 1 );
add_filter( 'wpforms_settings_defaults', [ $this, 'register_settings_fields' ], 5, 1 );
add_action( 'wpforms_settings_updated', [ $this, 'updated' ] );
add_action( 'wpforms_settings_enqueue', [ $this, 'enqueues' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'apply_noconflict' ], 9999 );
* Register CAPTCHA settings tab.
* @param array $tabs Admin area tabs list.
public function register_settings_tabs( $tabs ) {
'name' => esc_html__( 'CAPTCHA', 'wpforms-lite' ),
'submit' => esc_html__( 'Save Settings', 'wpforms-lite' ),
return wpforms_array_insert( $tabs, $captcha, 'email' );
* Register CAPTCHA settings fields.
* @param array $settings Admin area settings list.
public function register_settings_fields( $settings ) {
$settings[ self::VIEW ] = [
self::VIEW . '-heading' => [
'id' => self::VIEW . '-heading',
'content' => '<h4>' . esc_html__( 'CAPTCHA', 'wpforms-lite' ) . '</h4><p>' . esc_html__( 'A CAPTCHA is an anti-spam technique which helps to protect your website from spam and abuse while letting real people pass through with ease.', 'wpforms-lite' ) . '</p>',
'class' => [ 'wpforms-setting-captcha-heading', 'section-heading' ],
self::VIEW . '-provider' => [
'id' => self::VIEW . '-provider',
'hcaptcha' => 'hCaptcha',
'recaptcha' => 'reCAPTCHA',
'turnstile' => 'Turnstile',
'none' => esc_html__( 'None', 'wpforms-lite' ),
wp_kses( /* translators: %s - WPForms.com CAPTCHA comparison page URL. */
__( 'Not sure which service is right for you? <a href="%s" target="_blank" rel="noopener noreferrer">Check out our comparison</a> for more details.', 'wpforms-lite' ),
esc_url( wpforms_utm_link( 'https://wpforms.com/docs/setup-captcha-wpforms/', 'Settings - Captcha', 'Captcha Comparison Documentation' ) )
// Add settings fields for each of available captcha types.
foreach ( $this->captchas as $captcha ) {
$settings[ self::VIEW ] = array_merge( $settings[ self::VIEW ], $captcha->get_settings_fields() );
$settings[ self::VIEW ] = array_merge(
'recaptcha-noconflict' => [
'id' => 'recaptcha-noconflict',
'name' => esc_html__( 'No-Conflict Mode', 'wpforms-lite' ),
'desc' => esc_html__( 'Forcefully remove other CAPTCHA occurrences in order to prevent conflicts. Only enable this option if your site is having compatibility issues or instructed by support.', 'wpforms-lite' ),
self::VIEW . '-preview' =>
'id' => self::VIEW . '-preview',
'name' => esc_html__( 'Preview', 'wpforms-lite' ),
'content' => '<p class="desc wpforms-captcha-preview-desc">' . esc_html__( 'Please save settings to generate a preview of your CAPTCHA here.', 'wpforms-lite' ) . '</p>',
'class' => [ 'wpforms-hidden' ],
$this->settings['provider'] === 'hcaptcha' ||
$this->settings['provider'] === 'turnstile' ||
( $this->settings['provider'] === 'recaptcha' && $this->settings['recaptcha_type'] === 'v2' )
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
* Modify captcha settings data.
* @param array $data Array of settings.
'wpforms_admin_pages_settings_captcha_data',
'sitekey' => $this->settings['site_key'],
'theme' => $this->settings['theme'],
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
// Prepare HTML for CAPTCHA preview.
$placeholder_description = $settings[ self::VIEW ][ self::VIEW . '-preview' ]['content'];
$captcha_description = esc_html__( 'This CAPTCHA is generated using your site and secret keys. If an error is displayed, please double-check your keys.', 'wpforms-lite' );
$captcha_preview = sprintf(
'<div class="wpforms-captcha-container" style="pointer-events:none!important;cursor:default!important;">
<input type="text" name="wpforms-captcha-hidden" class="wpforms-recaptcha-hidden" style="position:absolute!important;clip:rect(0,0,0,0)!important;height:1px!important;width:1px!important;border:0!important;overflow:hidden!important;padding:0!important;margin:0!important;">
wpforms_html_attributes( '', [ 'wpforms-captcha', 'wpforms-captcha-' . $this->settings['provider'] ], $data )
$settings[ self::VIEW ][ self::VIEW . '-preview' ]['content'] = sprintf(
'<div class="wpforms-captcha-preview">
%1$s <p class="desc">%2$s</p>
<div class="wpforms-captcha-placeholder wpforms-hidden">%3$s</div>',
$settings[ self::VIEW ][ self::VIEW . '-preview' ]['class'] = [];
* Re-init CAPTCHA settings when plugin settings were updated.
public function updated() {
* Display notice about the CAPTCHA preview.
private function notice() {
if ( ! wpforms_is_admin_page( 'settings', self::VIEW ) || ! $this->is_captcha_preview_ready() ) {
Notice::info( esc_html__( 'A preview of your CAPTCHA is displayed below. Please view to verify the CAPTCHA settings are correct.', 'wpforms-lite' ) );
* Check if CAPTCHA config is ready to display a preview.
private function is_captcha_preview_ready() {
$current_captcha = $this->get_current_captcha();
if ( ! $current_captcha ) {
return $current_captcha->is_captcha_preview_ready();
* Enqueue assets for the CAPTCHA settings page.
public function enqueues() {
$current_captcha = $this->get_current_captcha();
if ( ! $current_captcha ) {
$current_captcha->enqueues();
* Get current active captcha object.
private function get_current_captcha() {
return ! empty( $this->captchas[ $this->settings['provider'] ] ) ? $this->captchas[ $this->settings['provider'] ] : '';
* Use the CAPTCHA no-conflict mode.
* When enabled in the WPForms settings, forcefully remove all other
* CAPTCHA enqueues to prevent conflicts. Filter can be used to target
public function apply_noconflict() {
! wpforms_is_admin_page( 'settings', self::VIEW ) ||
empty( wpforms_setting( 'recaptcha-noconflict' ) ) ||
* Allow/disallow applying non-conflict mode for captcha scripts.
* @param boolean $allow True/false. Default: true.
! apply_filters( 'wpforms_admin_settings_captcha_apply_noconflict', true ) // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
$urls = [ 'google.com/recaptcha', 'gstatic.com/recaptcha', 'hcaptcha.com/1', 'challenges.cloudflare.com/turnstile' ];
foreach ( $scripts->queue as $handle ) {
// Skip the WPForms JavaScript assets.
! isset( $scripts->registered[ $handle ] ) ||
false !== strpos( $scripts->registered[ $handle ]->handle, 'wpforms' )
foreach ( $urls as $url ) {
if ( false !== strpos( $scripts->registered[ $handle ]->src, $url ) ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );