Add support for hCaptcha

Hi Ardain,

Because I couldn’t wait any longer I implemented it myself :smiling_face_with_halo:
Here are my snippets (UPDATED):

// Add hCaptcha to user register form
add_filter(
	'hivepress/v1/forms/user_register',
	function( $form ) {
		$form['footer'] = '<div id="my-hcaptcha-box">' . do_shortcode('[hcaptcha]') . '</div>' . hivepress()->helper->get_array_value( $form, 'footer' );
		return $form;
	},
	100
);

// Throw error if hCaptcha is missing
add_filter(
	'hivepress/v1/forms/user_register/errors',
	function( $errors, $form ) {
		$result = \HCaptcha\Helpers\API::verify_request();		

		if ( null !== $result ) {
			$errors[] = 'Please solve the hCaptcha.';
		}		
		
		return $errors;
	},
	100,
	2
);

Please note I used the hook ‘hivepress/v1/forms/user_register’, but I’m sure you can also use ‘hivepress/v1/forms/user_login’ (did not test it though).

Because the above snippet inserts the hCaptcha below the submit button, I added a jQuery snippet as well, which also moves the form messages for better UX.

// Move hCaptcha and form messages above submit button
jQuery(document).ready(function( $ ) {
	// Get elements
	const $submitButton = $('#user_register_modal .hp-form__footer button');	
	const $formMessages = $('#user_register_modal .hp-form__messages');
	const $hCaptcha = $('#my-hcaptcha-box');
	
	// Insert elements	
	$hCaptcha.insertBefore($submitButton);
	$formMessages.insertBefore($submitButton);	
	
});

Hope this helps!

1 Like