Tell us what you are trying to do?
I have a custom text field.
I want to use [wpv-conditional] in the Content Template to check if the field contains a given string.
Is there any documentation that you are following?
I've tried to use solution provided here: https://toolset.com/forums/topic/conditional-display-of-content-condition-not-equal-but-contains-4/
but it doesn't work - for any given string I got the result that is not 'false' (so it says that the string exists, even if I know it doesn't).
This is the code:
[wpv-conditional if="( '[wpv_contains haystack='[types field='airline-aircraft' output='raw'][/types]' needle='777']' ne 'false' )"]
has 777
[/wpv-conditional]
[wpv-conditional if="( '[wpv_contains haystack='[types field='airline-aircraft' output='raw'][/types]' needle='xxxxx']' ne 'false' )"]
has xxxxx
[/wpv-conditional]
I registered 'wpv_contains' in Settings > Functions inside conditional evaluations.
I added the PHP snippet using WPCode plugin.
What is the link to your site?
You can check it here (the output from the code above can be seen just before the footer)
hidden link
Thank you.
Hi,
Thank you for contacting us and I'd be happy to assist.
I tested the slightly modified shortcode on my test website and it worked:
1. I registered the following custom shortcode:
// Add Shortcode
function wpv_contains( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'haystack' => '',
'needle' => '',
),
$atts
);
$pos = strpos($atts['haystack'], $atts['needle']);
if($pos == false){
return 0;
}
else {
return 1;
}
}
add_shortcode( 'wpv_contains', 'wpv_contains' );
2. Next, added the "wpv_contains" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.
3. This shortcode returns '0' if the target text is not found and '1' if it is found, so used in the conditional statement like this:
[wpv-conditional if="( '[wpv_contains haystack='[types field='field-slug'][/types]' needle='text to be found']' eq '1' )"]
Found the text!
[/wpv-conditional]
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Thank you, Waqar
I can confirm that the modified shortcode produces the correct output: 0 or 1.
However, when I use the shortcode inside [wpv-conditional]:
---
[wpv-conditional if="( '[wpv_contains haystack='[types field='airline-aircraft'][/types]' needle='Boeing']' eq '1' )"]
Found the text!
[/wpv-conditional]
[wpv-conditional if="( '[wpv_contains haystack='[types field='airline-aircraft'][/types]' needle='xxx']' eq '0' )"]
Not found
[/wpv-conditional]
---
nothing is displayed on the page 🙁
I double-checked if "wpv_contains" is properly registered.
What could go wrong here?
Thanks for the update and glad that the shortcode output is working correctly.
For the conditional statement, you can turn on the conditional debugging, by including the debug="true" attribute, to see the actual evaluations which are taking place:
https://toolset.com/documentation/legacy-features/views-plugin/debugging-views-output/
[wpv-conditional if="( '[wpv_contains haystack='[types field='airline-aircraft'][/types]' needle='Boeing']' eq '1' )" debug="true"]
Tip: If you're trying to use these statements inside an environment/builder from a third-party theme or plugin, you can trying including these through a Toolset content template, as suggested in this reply:
https://toolset.com/forums/topic/conditional-logic-to-check-if-a-user-is-logged-in-or-not/#post-2676767
Thank you.
I already use Content Template to generate my content.
I've added debug="true" as you recommended, however, I don't see any debug info on the page?
That is strange.
Can you please share temporary admin login details, along with the link to an example page where this conditions is not working as expected?
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.
Thank you for sharing these details.
During troubleshooting, I noticed that the Toolset content template is being loaded inside the WP Bakery plugin's content.
Further tests confirmed that it was having difficulty recognizing this complex conditional statement structure, where shortcodes within shortcodes were being used.
As an alternative, I've updated the custom shortcode 'wpv_contains', so that it needs one less level of nesting:
// Add Shortcode
function wpv_contains( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'field' => '',
'needle' => '',
),
$atts
);
$return = 0;
if(!empty($atts['field'])) {
$field_value = types_render_field( $atts['field'], array( "output" => "raw" ) );
if(!empty($field_value)) {
$pos = strpos($field_value, $atts['needle']);
if($pos !== false){
$return = 1;
}
}
}
return $return;
}
add_shortcode( 'wpv_contains', 'wpv_contains' );
It can now be used directly to find a 'needle' in the field's value, like this:
[wpv_contains field='airline-aircraft' needle='Boeing']
And you can see the following conditional statements working in the content template, using this updated shortcode structure:
[wpv-conditional if="( '[wpv_contains field='airline-aircraft' needle='Boeing']' eq '1' )"]
Text found!
[/wpv-conditional]
[wpv-conditional if="( '[wpv_contains field='airline-aircraft' needle='xxx']' eq '0' )"]
Text not found!
[/wpv-conditional]
Very clever solution and it works!
Thank you, Waqar.