: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
** A base module for [captchac] and [captchar]
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_captcha', 10, 0 );
function wpcf7_add_form_tag_captcha() {
// CAPTCHA-Challenge (image)
wpcf7_add_form_tag( 'captchac',
'wpcf7_captchac_form_tag_handler',
'zero-controls-container' => true,
// CAPTCHA-Response (input)
wpcf7_add_form_tag( 'captchar',
'wpcf7_captchar_form_tag_handler',
function wpcf7_captchac_form_tag_handler( $tag ) {
if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
/* translators: %s: link labeled 'Really Simple CAPTCHA' */
esc_html( __( "To use CAPTCHA, you need %s plugin installed.", 'contact-form-7' ) ),
wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' )
return sprintf( '<em>%s</em>', $error );
if ( empty( $tag->name ) ) {
$class = wpcf7_form_controls_class( $tag->type );
$class .= ' wpcf7-captcha-' . str_replace( ':', '', $tag->name );
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
'img_size' => array( 72, 24 ),
'base' => array( 6, 18 ),
$op = array_merge( $op, wpcf7_captchac_options( $tag->options ) );
if ( ! $filename = wpcf7_generate_captcha( $op ) ) {
if ( ! empty( $op['img_size'] ) ) {
if ( isset( $op['img_size'][0] ) ) {
$atts['width'] = $op['img_size'][0];
if ( isset( $op['img_size'][1] ) ) {
$atts['height'] = $op['img_size'][1];
$atts['alt'] = 'captcha';
$atts['src'] = wpcf7_captcha_url( $filename );
$atts = wpcf7_format_atts( $atts );
$prefix = substr( $filename, 0, strrpos( $filename, '.' ) );
'<input type="hidden" name="%1$s" value="%2$s" /><img %3$s />',
esc_attr( sprintf( '_wpcf7_captcha_challenge_%s', $tag->name ) ),
function wpcf7_captchar_form_tag_handler( $tag ) {
if ( empty( $tag->name ) ) {
$validation_error = wpcf7_get_validation_error( $tag->name );
$class = wpcf7_form_controls_class( $tag->type );
if ( $validation_error ) {
$class .= ' wpcf7-not-valid';
$atts['size'] = $tag->get_size_option( '40' );
$atts['maxlength'] = $tag->get_maxlength_option();
$atts['minlength'] = $tag->get_minlength_option();
if ( $atts['maxlength'] and $atts['minlength']
and $atts['maxlength'] < $atts['minlength'] ) {
unset( $atts['maxlength'], $atts['minlength'] );
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
$atts['autocomplete'] = 'off';
if ( $validation_error ) {
$atts['aria-invalid'] = 'true';
$atts['aria-describedby'] = wpcf7_get_validation_error_reference(
$atts['aria-invalid'] = 'false';
$value = (string) reset( $tag->values );
if ( wpcf7_is_posted() ) {
if ( $tag->has_option( 'placeholder' )
or $tag->has_option( 'watermark' ) ) {
$atts['placeholder'] = $value;
$atts['name'] = $tag->name;
'<span class="wpcf7-form-control-wrap" data-name="%1$s"><input %2$s />%3$s</span>',
wpcf7_format_atts( $atts ),
add_filter( 'wpcf7_validate_captchar',
'wpcf7_captcha_validation_filter', 10, 2 );
function wpcf7_captcha_validation_filter( $result, $tag ) {
$captchac = '_wpcf7_captcha_challenge_' . $name;
$prefix = (string) ( $_POST[$captchac] ?? '' );
$response = (string) ( $_POST[$name] ?? '' );
$response = wpcf7_canonicalize( $response );
if ( 0 === strlen( $prefix )
or ! wpcf7_check_captcha( $prefix, $response ) ) {
$result->invalidate( $tag, wpcf7_get_message( 'captcha_not_match' ) );
if ( 0 !== strlen( $prefix ) ) {
wpcf7_remove_captcha( $prefix );
add_filter( 'wpcf7_refill_response', 'wpcf7_captcha_ajax_refill', 10, 1 );
add_filter( 'wpcf7_feedback_response', 'wpcf7_captcha_ajax_refill', 10, 1 );
function wpcf7_captcha_ajax_refill( $items ) {
if ( ! is_array( $items ) ) {
$tags = wpcf7_scan_form_tags( array( 'type' => 'captchac' ) );
foreach ( $tags as $tag ) {
$options = $tag->options;
$op = wpcf7_captchac_options( $options );
if ( $filename = wpcf7_generate_captcha( $op ) ) {
$captcha_url = wpcf7_captcha_url( $filename );
$refill[$name] = $captcha_url;
if ( ! empty( $refill ) ) {
$items['captcha'] = $refill;
add_filter( 'wpcf7_messages', 'wpcf7_captcha_messages', 10, 1 );
function wpcf7_captcha_messages( $messages ) {
$messages = array_merge( $messages, array(
'captcha_not_match' => array(
__( "The code that sender entered does not match the CAPTCHA", 'contact-form-7' ),
__( 'Your entered code is incorrect.', 'contact-form-7' ),
add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_captcha', 46, 0 );
function wpcf7_add_tag_generator_captcha() {
if ( ! wpcf7_use_really_simple_captcha() ) {
$tag_generator = WPCF7_TagGenerator::get_instance();
$tag_generator->add( 'captcha',
__( 'CAPTCHA (Really Simple CAPTCHA)', 'contact-form-7' ),
'wpcf7_tag_generator_captcha' );
function wpcf7_tag_generator_captcha( $contact_form, $args = '' ) {
$args = wp_parse_args( $args, array() );
if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
<div class="control-box">
/* translators: %s: link labeled 'Really Simple CAPTCHA' */
esc_html( __( "To use CAPTCHA, you first need to install and activate %s plugin.", 'contact-form-7' ) ),
wpcf7_link( 'https://wordpress.org/plugins/really-simple-captcha/', 'Really Simple CAPTCHA' )
$description = __( "Generate form-tags for a CAPTCHA image and corresponding response input field. For more details, see %s.", 'contact-form-7' );
$desc_link = wpcf7_link( __( 'https://contactform7.com/captcha/', 'contact-form-7' ), __( 'CAPTCHA', 'contact-form-7' ) );
<div class="control-box">
<legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend>
<table class="form-table">
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th>
<td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td>
<table class="form-table scope captchac">
<caption><?php echo esc_html( __( "Image settings", 'contact-form-7' ) ); ?></caption>
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
<td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-id' ); ?>" /></td>
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
<td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchac-class' ); ?>" /></td>
<table class="form-table scope captchar">
<caption><?php echo esc_html( __( "Input field settings", 'contact-form-7' ) ); ?></caption>
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th>
<td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-id' ); ?>" /></td>
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th>
<td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-captchar-class' ); ?>" /></td>
<input type="text" name="captcha" class="tag code" readonly="readonly" onfocus="this.select()" />
<input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" />
add_action( 'wpcf7_admin_warnings',
'wpcf7_captcha_display_warning_message', 10, 3 );
function wpcf7_captcha_display_warning_message( $page, $action, $object ) {
if ( $object instanceof WPCF7_ContactForm ) {
$has_tags = (bool) $contact_form->scan_form_tags(
array( 'type' => array( 'captchac' ) ) );
if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
$uploads_dir = wpcf7_captcha_tmp_dir();
if ( ! is_dir( $uploads_dir )
or ! wp_is_writable( $uploads_dir ) ) {
$message = sprintf( __( 'This contact form contains CAPTCHA fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.', 'contact-form-7' ), $uploads_dir );
echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>';
if ( ! function_exists( 'imagecreatetruecolor' )
or ! function_exists( 'imagettftext' ) ) {
$message = __( "This contact form contains CAPTCHA fields, but the necessary libraries (GD and FreeType) are not available on your server.", 'contact-form-7' );
echo '<div class="notice notice-warning"><p>' . esc_html( $message ) . '</p></div>';
function wpcf7_init_captcha() {
if ( class_exists( 'ReallySimpleCaptcha' ) ) {
$captcha = new ReallySimpleCaptcha();
$dir = trailingslashit( wpcf7_captcha_tmp_dir() );
$captcha->tmp_dir = $dir;
if ( is_callable( array( $captcha, 'make_tmp_dir' ) ) ) {
$result = $captcha->make_tmp_dir();
$result = wp_mkdir_p( $dir );
$htaccess_file = path_join( $dir, '.htaccess' );
if ( file_exists( $htaccess_file ) ) {
list( $first_line_comment ) = (array) file(
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
if ( '# Apache 2.4+' === $first_line_comment ) {
if ( $handle = @fopen( $htaccess_file, 'w' ) ) {
fwrite( $handle, "# Apache 2.4+\n" );
fwrite( $handle, "<IfModule authz_core_module>\n" );
fwrite( $handle, " Require all denied\n" );
fwrite( $handle, ' <FilesMatch "^\w+\.(jpe?g|gif|png)$">' . "\n" );
fwrite( $handle, " Require all granted\n" );
fwrite( $handle, " </FilesMatch>\n" );
fwrite( $handle, "</IfModule>\n" );
fwrite( $handle, "# Apache 2.2\n" );
fwrite( $handle, "<IfModule !authz_core_module>\n" );
fwrite( $handle, " Order deny,allow\n" );
fwrite( $handle, " Deny from all\n" );
fwrite( $handle, ' <FilesMatch "^\w+\.(jpe?g|gif|png)$">' . "\n" );
fwrite( $handle, " Allow from all\n" );
fwrite( $handle, " </FilesMatch>\n" );
fwrite( $handle, "</IfModule>\n" );
* Returns the directory path for Really Simple CAPTCHA files.
* @return string Directory path.
function wpcf7_captcha_tmp_dir() {
if ( defined( 'WPCF7_CAPTCHA_TMP_DIR' ) ) {
$dir = path_join( WP_CONTENT_DIR, WPCF7_CAPTCHA_TMP_DIR );
if ( wpcf7_is_file_path_in_content_dir( $dir ) ) {
$dir = path_join( wpcf7_upload_dir( 'dir' ), 'wpcf7_captcha' );
function wpcf7_captcha_tmp_url() {
if ( defined( 'WPCF7_CAPTCHA_TMP_URL' ) ) {
return WPCF7_CAPTCHA_TMP_URL;
return path_join( wpcf7_upload_dir( 'url' ), 'wpcf7_captcha' );
function wpcf7_captcha_url( $filename ) {
$url = path_join( wpcf7_captcha_tmp_url(), $filename );
and 'http:' == substr( $url, 0, 5 ) ) {
$url = 'https:' . substr( $url, 5 );
return apply_filters( 'wpcf7_captcha_url', sanitize_url( $url ) );
function wpcf7_generate_captcha( $options = null ) {
if ( ! $captcha = wpcf7_init_captcha() ) {