• Resolved jbslettebak

    (@jbslettebak)


    Hello,

    I am running a custom function in my functions.php file that will require either the tel field or the email field to be required (user must fill in one or the other). I want this function to run on all forms except for one.

    What I’m looking for is some snippet of code that I can use to check the ID of all the forms and then have this function run on all the forms except for the one I want to exclude.

    Here is the function I’m using:

    add_filter( ‘wpcf7_validate_tel’, ‘wpcf7_text_validation_filter’, 10, 2 );
    add_filter( ‘wpcf7_validate_tel*’, ‘wpcf7_text_validation_filter’, 10, 2 );
    add_filter( ‘wpcf7_validate_email’, ‘my_site_conditional_required’, 10, 2 );
    add_filter( ‘wpcf7_validate_email*’, ‘my_site_conditional_required’, 10, 2 );

    function my_site_conditional_required($result, $tag) {
    $tag = new WPCF7_Shortcode( $tag );

    $name = $tag->name;

    $value = isset( $_POST[$name] )
    ? trim( wp_unslash( strtr( (string) $_POST[$name], ‘\n’, ‘ ‘ ) ) )
    : ”;

    if ( ‘your-phone’ == $name ) :
    if ( ” == $value && ” == $_POST[‘your-email’] ) :
    $result->invalidate( $tag, wpcf7_get_message( ‘invalid_required’ ) );
    endif;
    endif;

    if ( ‘your-email’ == $name ) :
    if ( ” == $value && ” == $_POST[‘your-phone’] ) :
    $result->invalidate( $tag, wpcf7_get_message( ‘invalid_required’ ) );
    endif;
    endif;

    return $result;
    }

    Please let me know if you have any questions.

    Thank you!

    • This topic was modified 4 years, 3 months ago by jbslettebak.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Within a validation filter function you can retrieve the ID of the contact form by doing like the following:

    $submission = WPCF7_Submission::get_instance();
    $contact_form = $submission->get_contact_form();
    $contact_form_id = $contact_form->id();
    Thread Starter jbslettebak

    (@jbslettebak)

    Great, thank you for the help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude form ID from function’ is closed to new replies.