How to Change the Captcha Math for the Custom Captcha Addon

Introduction

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 or to limit the calculation method itself, you can easily do this with a custom code snippet.

By default, the Custom Captcha addon uses addition and multiplication for it’s math with number ranging from 1 to 15.

In this tutorial, we’ll show you how to use PHP to change the minimum and maximum numbers used as well as the calculation method using PHP.

Creating the form

To begin, you’ll need to create a new form and add your Custom Captcha form field to the form.

If you need any help in how to set up a form using the Custom Captcha form field, please review this documentation.

create your form and add your custom captcha form field

Adding the snippet

Below, we’re going to show you two different snippets. One will just change the maximum number that the equation uses and one will change the calculation to additional only.

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

If you need any assistance in how to add snippets to your site, please review this tutorial.

Changing the max number

The example shown below will set the maximum number for the equation to 4.

/**
 * Adjust math captcha settings.
 *
 * @link   https://wpforms.com/developers/change-the-captcha-math-for-the-custom-captcha-addon/
 */

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', 30, 1 );

Using only addition

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

In this example, we’re changing the calculation to only use addition.

/**
 * Adjust math captcha settings.
 *
 * @link   https://wpforms.com/developers/change-the-captcha-math-for-the-custom-captcha-addon/
 */

function wpf_dev_math_captcha_settings( $settings ) {
	
	/* Default settings
		array(
			'min' => 1,
			'max' => 15,
			'cal' => [ '+', '*' ],
		);
	 */

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

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

using this snippet you can change the captcha math

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