0

I'm trying to change the filter for email so that it reads "Email Address is Required". I check to is if it is empty().

My tag in Contact Form 7 is [email* your-email]

You can see here, I tried everything to change the filter response but nothing works.

add_filter( 'wpcf7_validate_email', 'custom_email_confirmation_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2 );

function custom_email_confirmation_validation_filter( $result, $tag ) {
    $result->invalidate( $tag, "Email Address is Required" );
    if ( 'your-email' == $tag->name ) {
        $your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';

        if ( empty($your_email) ) {
            $result->invalidate( $tag, "Email Address is Required" );
        } else {
             $result->invalidate( $tag, "Email Address is Required" );
        }
    }

    return $result;
}

What might I be missing?

Thanks

closed as off-topic by Michael, Jacob Peattie, bosco, nmr, fuxia Aug 7 at 9:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • I noticed, my custom validation works if the field is not marked as required. Not sure why that is, as the filter is exactly how the contact form 7 documentation is. – Best Dev Tutorials Jul 6 at 15:43
1

First of all if you are already checking if the field is empty with that function, there's no need to use * in the backoffice [email* your-email].

In any case, it's not working because is_required()executes first than your function.

Solutions:

1) Remove the * in the backend

OR

2) Change the priority

// From 20 to 5 should work
// You only need to apply this to this filter as it is the one for required fields

add_filter( 'wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 5, 2 );

Hope it helps!

  • How did you know that 5 would work for priority? – Best Dev Tutorials Jul 9 at 20:05
  • @BestDevTutorials actually I can't be 100% sure it will work for everyone, but I know that: 1 - when you an * to a field in a Contact Form 7 shortecode, it will trigger a function | 2 - that function has a lower priority then 5 | 3 - Unless you have another filter with higher priority than 5 it should work just fine – João Teixeira Jul 9 at 23:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.