How to Change the Captcha Math for the Custom Captcha Addon

Overview

Would you like to change the captcha math for your visitors with the Custom Captcha addon? If you wish to change the math used or limit the range of numbers used in the equation you can easily do this with a custom code snippet. In this tutorial, we’ll show you how to use PHP to change the minimum and maximum numbers ursed as well as the calculation using PHP.

The Custom Captcha Addon can be set up to display a math captcha for users to solve.

As a result, you could change the captcha to only use numbers between 1 and 50. You can also change the captcha to only use addition.

Implementation Options

There are two different ways this can be applied:

  1. Change the maximum number used in a math captcha
  2. Use only addition in math captcha

Simply choose which one of the options below you’d like to use and add the code to your site.

1) Change the max number

The example shown below will set the ['max'] to 4.

/**
 * Adjust math captcha settings.
 *
 * @link   https://wpforms.com/developers/change-the-captcha-math-for-the-custom-captcha-addon/
 *
 * @param  array $settings
 * @return array
 */
function wpf_dev_math_captcha_settings( $settings ) {
	
	/* Default settings
		array(
			'min' => 1,
			'max' => 15,
			'cal' => array( '+', '*' ),
		);
	 */

	// Change max number 4 for easier math
	$settings['max'] = 4;

	return $settings;
}
add_filter( 'wpforms_math_captcha', 'wpf_dev_math_captcha_settings' );

2) Use only addition in math captcha

Similarly, you can also change the captcha to only use addition instead of addition and multiplication.

In the example below, we’re telling captcha to only use addition for the calculation.

/**
 * Adjust math captcha settings.
 *
 * @link   https://wpforms.com/developers/change-the-captcha-math-for-the-custom-captcha-addon/
 *
 * @param  array $settings
 * @return array
 */
function wpf_dev_math_captcha_settings( $settings ) {
	
	/* Default settings
		array(
			'min' => 1,
			'max' => 15,
			'cal' => array( '+', '*' ),
		);
	 */

    // Use addition only for easier math
	$settings['cal'] = '+';

	return $settings;
}
add_filter( 'wpforms_math_captcha', 'wpf_dev_math_captcha_settings' );

And that’s it! With this code snippet, you can now change the numbers or the calculation used in the Custom Captcha addon. Would you like to limit the number of characters a user can enter in your form field? Have a look at our article on How to Limit the Number of Characters for a Text Field.

Filter reference: wpforms_math_captcha