詳細検索

Check in Contact Form DB

Avatar
by すがピー
2 min read

Check in Contact Form DB
Translated from 日本語 • View original

If you want to do some kind of check before inserting into a table in Contact Form DB, use hooks.

function my_form_chk( $cf7 ) {

Retrieving Input Form Values
            foreach ($cf7->posted_data as $posted_name => $posted_value) {
                The original value does not change, so it is changed to a different variable
                $posted_name_2 = $posted_name;
                $posted_value_2 = $posted_value;

Shaping values
                $posted_nameClean = stripslashes($posted_name_2);

$posted_value_2 = is_array($posted_value_2) ? implode($posted_value_2, ', ') : $posted_value_2;
                $posted_valueClean = stripslashes($posted_value_2);

Assign to each variable
                if( $posted_nameClean == 'a' ) {
                    $input_param_a = $posted_valueClean;
                }
                if( $posted_nameClean == 'b' ) {
                    $input_param_b = $posted_valueClean;
                }
                if( $posted_nameClean == 'c' ) {
                    $input_param_c = $posted_valueClean;
                }

}

Check Processing
            if ( $input_param_a !== $input_param_b || $input_param_a !== $input_param_c ) {
                  $cf7->skip_mail = true;
            }

}

add_action('wpcf7_before_send_mail', 'my_form_chk', 1);

In the example above, there are three input fields in the input form, a, b, and c, and we take the values and assign true to $cf 7->skip_mail if we do not specify a and b or a and c match.

This will skip sending emails for Contact Form 7. However, the registration in the DB does not stop.

Therefore, we will modify some of the Sources of Contact Form DB.

/wp-content/plugins/contact-form-7-to-database-extension CF7DBPlugin.php add the following action near line 467:

            if ($cf7->skip_mail) {
                return; // Don't save in DB
            }

Simply put, if $cf 7->skip_mail is assigned true, it will finish processing the Contact Form DB.

This stops inserting into the DB, but if you look at it in the browser, a message will be output as if it has been registered.

Therefore, we will modify some of the Sources of Contact Form 7.

/wp-content/plugins/contact-form-7/includes classes.php Change the processing around line 467 to the following:

//     if ( $this->skip_mail )
//          return true;
        if ( $this->skip_mail ) {
            return false;
        }

This is because $cf 7->skip_mail is originally a flag that suppresses email sending, but when it is true, it returns true, so it looks successful when viewed in the browser.

So, if you return false when $cf 7->skip_mail is true (when it gets stuck in the check process), you will get an error.

By the way, if you don't want to send an email in the first place, you can use

//     if ( $this->skip_mail )
//          return true;
        if ( $this->skip_mail ) {
            return false;
        } else {
            return true;
        }

If you return true with else, if it does not get caught in the check process, a success message will be displayed after registering the DB.

Related Articles