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:
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.
Change:
on:
Because your
if
statement is alwaysTrue
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:
Into
At least,
in_array
is the best way to check for multiple values. So that should be kept in your final code!Your second attempted (with the
||
) is outright invalid. Check out PHP’s operator precedence rules: http://php.net/manual/en/language.operators.precedence.phpThe code is being parsed/executed as:
(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: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. Usein_array()
, instead of!in_array()
.