PHP syntax help for checking multiple IDS in an array

I am using contact form 7 in wordpress and had to create a custom hook for the form action url. I have it working if I want to check if a particular forms “id” exist, and if so to send it to another url. That code looks like this:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');

function wpcf7_custom_form_action_url($url)
{
    global $wpcf7_contact_form;

    if ($wpcf7_contact_form->id === 333)
    {
        return 'http://mydomain.com/Leads/';
    }
    else
    {
        return $url;
    }
}

However, I have 4 forms that I want it to check to see if any of them exist to send the to the same url as above (where its my domain url). If I try adding multiple ids it breaks. This is what I have tried:

Read More
add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');

function wpcf7_custom_form_action_url($url)
{
    global $wpcf7_contact_form;

    if ($wpcf7_contact_form->id === 333 || 334 || 335 || 336)
    {
        return 'http://mydomain.com//Leads/';
    }
    else
    {
        return $url;
    }
}

The code above seems to not work.

I have also tried to check it as an array like this:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');

function wpcf7_custom_form_action_url($url)
{
    global $wpcf7_contact_form;

    if( !in_array($wpcf7_contact_form->id,array(660, 684, 685, 686)))
    {
        return 'http://mydomain.com/Leads/';
    }
    else
    {
        return $url;
    }
}

Again, it appears that this is not correct. Can anyone see anything that is incorrect above. I can’t seem to get it working.

Related posts

Leave a Reply

3 comments

  1. Change:

    if ($wpcf7_contact_form->id === 333 || 334 || 335 || 336)
    

    on:

    if (in_array($wpcf7_contact_form->id, array(333, 334, 335, 336)))
    

    Because your if statement is always True

  2. You wish to check forms with e.g. IDs (660, 684, 685, 686) right?

    If that is the case, you have inverted the in_array statement (! in_array)

    Change:

    if( !in_array($wpcf7_contact_form->id,array(660, 684, 685, 686)))
    

    Into

    if(in_array($wpcf7_contact_form->id,array(660, 684, 685, 686)))
    

    At least, in_array is the best way to check for multiple values. So that should be kept in your final code!

  3. Your second attempted (with the ||) is outright invalid. Check out PHP’s operator precedence rules: http://php.net/manual/en/language.operators.precedence.php

    The code is being parsed/executed as:

    if (($wpcf7_contact_form->id === 333) || 334 || 335 || 336)
        ^--                             ^--
    

    (note the extra brackets). The equality test is performed and returns a true/false value, which you then do a logical OR operation with a series of integers:

    0 || 334 || 335 || 336  -> 0 || 351 -> 351 -> true
    1 || 334 || 335 || 336  -> 1 || 351 -> 351 -> true
    

    Your in_array() version is correct, except you’re !-notting the in_array test, so you redirect if the retrieved id ISN’T one of those values. If I’m reading your question right, that’s the opposite of what you want. Use in_array(), instead of !in_array().