A
A
ak_wi2018-02-03 21:03:49
WordPress
ak_wi, 2018-02-03 21:03:49

How to abort a Contact Form 7 submission?

I use the following hook for Contcact Form 7

add_action("wpcf7_posted_data", "wpcf7_modify_this");
function wpcf7_modify_this($posted_data) {

  
      $url = strpos($posted_data['your-message'], 'http://');
    
    if ($url) {
      return false;
    }
  

  return $posted_data;
}

Required instead of return false; do a reset of the form submission (so that the letter is not sent) or an error output. Can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2018-02-03
@ak_wi

You need to use the wpcf7_validate_ dynamic hook.
More or less like this:

add_filter( 'wpcf7_validate_textarea', 'custom_textarea_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 20, 2 );
 
function custom_textarea_validation_filter( $result, $tag ) {
    if ( 'your-message' == $tag->name ) {
        $your_message = isset( $_POST['your-message'] ) ? trim( $_POST['your-message'] ) : '';
 
        if ( strpos($your_message, 'http://') !== false ) {
            $result->invalidate( $tag, "Incorrect value" );
        }
    }
 
    return $result;
}

Documentation for creating your own validation rules .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question