Home › Toolset Professional Support › [Resolved] Conditional check if value is contained in a comma-separated list
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
Tagged: Views, Views plugin
This topic contains 13 replies, has 2 voices.
Last updated by alessandroC-2 6 years, 4 months ago.
Assisted by: Christian Cox.
Tell us what you are trying to do?
On a local site I'm trying to create a membership site with Types, Layouts, Views and the WP eMember plugin.
I created one View to use on a page and display a post archive where users can purchase access for each post.
Each post represents one membership level and therefore users will have more membership level (one primary and many secondary).
I added a custom field to each post named Membership Name.
I would like to show an image with links to the post if the user has purchased access, and a purchase button and a post blocked image if the user has not bought access.
I use these two conditions
[wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' eq '[types field=''membership-name'][/types]' ) OR ( '[wp_eMember_user_details user_info="user_additional_membership_level_names"]' eq '[types field='membership-name'][/types]' )"] <a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-featured-image size="custom" width="214" height="214" crop="true"]</a> [wpv-post-taxonomy type="category" format="name"] [/wpv-conditional] [wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' ne '[types field=''membership-name'][/types]' ) AND ( '[wp_eMember_user_details user_info="user_additional_membership_level_names"]' ne '[types field='membership-name'][/types]' )"] [types field='post-locked' title='%%TITLE%%' alt='%%ALT%%' size='custom' width='214' height='214' resize='crop'][/types] [types field='buy-access-button'][/types] [/wpv-conditional]
Everything works until a user has more than 3 membership levels as in that case the
[wp_eMember_user_details user_info="user_additional_membership_level_names"]
shows a comma separated list of membership levels (for example Silver,Gold,Platinum), for this reason I would need to find a way to check if this string contains one of the values defined by the below shortcode.
[types field=''membership-name'][/types]
Is there any chance that a solution can be found or should I find an alternative solution? Thank you
What is the link to your site?
At the time I was developing this part on a local site before implementing it on an active site.
Hi, here is an example showing how you can use the "ARRAY" operator and the "eq" comparison in a conditional to test if a single value exists in a comma-separated list of values:
[wpv-conditional if="(ARRAY(1,2,3) eq '1')"] 1 is in the array [/wpv-conditional] [wpv-conditional if="(ARRAY(1,2,3) eq '2')"] 2 is in the array [/wpv-conditional] [wpv-conditional if="(ARRAY(1,2,3) eq '3')"] 3 is in the array [/wpv-conditional] [wpv-conditional if="(ARRAY(1,2,3) eq '4')"] 4 is not in the array so this should not be displayed [/wpv-conditional]
So if you have a shortcode that can produce a comma-separated string like 1,2,3 you can insert that inside the ARRAY parentheses, paying close attention to nested quotes. You must add that shortcode name in Toolset > Settings > Frontend Content > Functions inside conditional evaluations. Then add your membership field to the conditional using the output='raw' attribute for best results:
[types field='membership-name' output='raw'][/types]
If that's not working as expected, add the debug='true' attribute to the conditional shortcode and copy + paste the results here for me to review along with the conditional code.
[wpv-conditional if="(ARRAY(1,2,3) eq '1')" debug="true"] 1 is in the array [/wpv-conditional]
Hi Christian, thank you for your response.
I tried your suggestions, but I don't get the desired results.
This is the conditional code:
[wpv-conditional if="( ARRAY('[wp_eMember_user_details user_info="user_additional_membership_level_names"]') eq '[types field='membership-name' output='raw'][/types]' ) OR ( '[wp_eMember_user_details user_info="user_membership_level_name"]' eq '[types field='membership-name' output='raw'][/types]' )"] <a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-featured-image size="custom" width="214" height="214" crop="true"]</a> [/wpv-conditional] [wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' ne '[types field='membership-name' output='raw'][/types]' ) AND (ARRAY('[wp_eMember_user_details user_info="user_additional_membership_level_names"]') ne '[types field='membership-name' output='raw'][/types]' )"] [types field='post-locked' title='%%TITLE%%' alt='%%ALT%%' size='custom' width='214' height='214' resize='crop'][/types] [types field='buy-access-button'][/types] [/wpv-conditional]
Testing on an account that has these membership levels: Admin, Platinum and Gold.
It seems that everything doesn't work on the array, this below is the debug result of the three membership levels.
#################### wpv-conditional attributes #################### Array ( [if] => ( ARRAY('Platinum,Gold') = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( ARRAY('Platinum,Gold') = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) -------------------- After replacing 1 general variables and comparing strings: ( ARRAY('Platinum,Gold') = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) Comparing Admin to Platinum #################### wpv-conditional attributes #################### Array ( [if] => ( 'Admin' ne 'Platinum' ) AND (ARRAY('Platinum,Gold') ne 'Platinum' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( 'Admin' ne 'Platinum' ) AND (ARRAY('Platinum,Gold') ne 'Platinum' ) -------------------- After replacing 1 general variables and comparing strings: ( 'Admin' ne 'Platinum' ) AND (ARRAY('Platinum,Gold') ne 'Platinum' ) Comparing Admin to Platinum
#################### wpv-conditional attributes #################### Array ( [if] => ( ARRAY('Platinum,Gold') = 'Admin' ) OR ( 'Admin' = 'Admin' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( ARRAY('Platinum,Gold') = 'Admin' ) OR ( 'Admin' = 'Admin' ) -------------------- After replacing 1 general variables and comparing strings: ( ARRAY('Platinum,Gold') = 'Admin' ) OR ( 'Admin' = 'Admin' ) Comparing Admin to Admin #################### wpv-conditional attributes #################### Array ( [if] => ( 'Admin' ne 'Admin' ) AND (ARRAY('Platinum,Gold') ne 'Admin' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( 'Admin' ne 'Admin' ) AND (ARRAY('Platinum,Gold') ne 'Admin' ) -------------------- After replacing 1 general variables and comparing strings: ( 'Admin' ne 'Admin' ) AND (ARRAY('Platinum,Gold') ne 'Admin' ) Comparing Admin to Admin
#################### wpv-conditional attributes #################### Array ( [if] => ( ARRAY('Platinum,Gold') = 'Gold' ) OR ( 'Admin' = 'Gold' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( ARRAY('Platinum,Gold') = 'Gold' ) OR ( 'Admin' = 'Gold' ) -------------------- After replacing 1 general variables and comparing strings: ( ARRAY('Platinum,Gold') = 'Gold' ) OR ( 'Admin' = 'Gold' ) Comparing Admin to Gold #################### wpv-conditional attributes #################### Array ( [if] => ( 'Admin' ne 'Gold' ) AND (ARRAY('Platinum,Gold') ne 'Gold' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( 'Admin' ne 'Gold' ) AND (ARRAY('Platinum,Gold') ne 'Gold' ) -------------------- After replacing 1 general variables and comparing strings: ( 'Admin' ne 'Gold' ) AND (ARRAY('Platinum,Gold') ne 'Gold' ) Comparing Admin to Gold
The single quotes inside the ARRAY syntax are causing the comma-separated lists to be interpreted as a single item instead of two items. Remove the single quotes wrapping the shortcode inside the ARRAY and re-nest the quotation marks in the user details shortcodes:
[wpv-conditional if="( ARRAY'[wp_eMember_user_details user_info='user_additional_membership_level_names']) eq '[types field='membership-name' output='raw'][/types]' ) OR ( '[wp_eMember_user_details user_info="user_membership_level_name"]' eq '[types field='membership-name' output='raw'][/types]' )"] <a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-featured-image size="custom" width="214" height="214" crop="true"]</a> [/wpv-conditional] [wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' ne '[types field='membership-name' output='raw'][/types]' ) AND (ARRAY([wp_eMember_user_details user_info='user_additional_membership_level_names']") ne '[types field='membership-name' output='raw'][/types]' )"] [types field='post-locked' title='%%TITLE%%' alt='%%ALT%%' size='custom' width='214' height='214' resize='crop'][/types] [types field='buy-access-button'][/types] [/wpv-conditional]
Hi Christian,
I tried your suggestions, but it still seems to work just only for the primary membership level and and not for those included in the array.
I enclose a debug of a secondary membership, that even if it is active for the user, it is not detected by the conditional check.
#################### wpv-conditional attributes #################### Array ( [if] => ( ARRAY '(Platinum,Gold)' = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( ARRAY '(Platinum,Gold)' = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) -------------------- After replacing 1 general variables and comparing strings: ( ARRAY '(Platinum,Gold)' = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) Comparing (Platinum,Gold) to Platinum After replacing 2 general variables and comparing strings: ( ARRAY '(Platinum,Gold)' = 'Platinum' ) OR ( 'Admin' = 'Platinum' ) Comparing Admin to Platinum #################### wpv-conditional attributes #################### Array ( [if] => ( ARRAY '(Platinum,Gold)' ne 'Platinum' ) AND ( 'Admin' ne 'Platinum' ) [debug] => true ) #################### Debug information #################### -------------------- Original expression: ( ARRAY '(Platinum,Gold)' ne 'Platinum' ) AND ( 'Admin' ne 'Platinum' ) -------------------- After replacing 1 general variables and comparing strings: ( ARRAY '(Platinum,Gold)' ne 'Platinum' ) AND ( 'Admin' ne 'Platinum' ) Comparing (Platinum,Gold) to Platinum After replacing 2 general variables and comparing strings: ( ARRAY '(Platinum,Gold)' ne 'Platinum' ) AND ( 'Admin' ne 'Platinum' ) Comparing Admin to Platinum
Sorry, I think I made transcription mistakes. Try this instead:
[wpv-conditional if="( ARRAY([wp_eMember_user_details user_info='user_additional_membership_level_names']) eq '[types field='membership-name' output='raw'][/types]' ) OR ( '[wp_eMember_user_details user_info="user_membership_level_name"]' eq '[types field='membership-name' output='raw'][/types]' )"] <a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-featured-image size="custom" width="214" height="214" crop="true"]</a> [/wpv-conditional] [wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' ne '[types field='membership-name' output='raw'][/types]' ) AND (ARRAY([wp_eMember_user_details user_info='user_additional_membership_level_names']) ne '[types field='membership-name' output='raw'][/types]' )"] [types field='post-locked' title='%%TITLE%%' alt='%%ALT%%' size='custom' width='214' height='214' resize='crop'][/types] [types field='buy-access-button'][/types] [/wpv-conditional]
Tried also this, it doesn't work.
Nothing appears in the view now.
Is it possible for me to log in and take a closer look? Might be faster than trading comments back and forth. If so, please use the private reply fields here to provide login credentials, and let me know where I can see this conditional on your site.
I'm sorry, not at the moment, because to avoid problems I'm working on a local site.
[wpv-conditional if="( ARRAY([wp_eMember_user_details user_info='user_additional_membership_level_names']) eq '[types field='membership-name' output='raw'][/types]' ) OR ( '[wp_eMember_user_details user_info="user_membership_level_name"]' eq '[types field="membership-name" output="raw"][/types]' )" debug='true'] <a href="[wpv-post-taxonomy type="category" format="url"]">[wpv-post-featured-image size="custom" width="214" height="214" crop="true"]</a> [/wpv-conditional] [wpv-conditional if="( '[wp_eMember_user_details user_info="user_membership_level_name"]' ne '[types field="membership-name" output="raw"][/types]' ) AND (ARRAY([wp_eMember_user_details user_info='user_additional_membership_level_names']) ne '[types field="membership-name" output="raw"][/types]' )" debug='true'] [types field='post-locked' title='%%TITLE%%' alt='%%ALT%%' size='custom' width='214' height='214' resize='crop'][/types] [types field='buy-access-button'][/types] [/wpv-conditional]
Okay I see one problem, the shortcode wp_eMember_user_details must be registered as a third-party shortcode argument in Toolset > Settings > Front-end content, but the name of the shortcode contains an uppercase letter. This isn't supported in the GUI, since the Shortcode API says shortcodes should be lowercase: https://codex.wordpress.org/Shortcode_API#Overview
You'll have to add this custom code snippet to register the function programmatically:
add_filter('wpv_custom_inner_shortcodes', 'prefix_add_my_shortcodes'); function prefix_add_my_shortcodes($shortcodes) { $shortcodes[] = 'wp_eMember_user_details'; return $shortcodes; }
If this doesn't work, we need to replicate your current setup on a testing site somewhere so we can work on it together.
Hi Christian,
it seems that in the conditional check Views see the array as a single element and not as different voices.
I created a testing site on a free hosting replicating the current setup.
The page where the view is shown is
hidden link
Username test123
Password test123
Okay I see the issue more clearly now. The eMember shortcode doesn't produce a list of strings in a format that can be used by the ARRAY operator in conditional HTML. If it produced strings in quotation marks like this:
'Platinum','Gold'
...then the ARRAY syntax would interpret those as separate items. Since they are not enclosed individually, the parser interprets them as one value. The reason my examples didn't show this problem is that I was testing numbers, which don't have to be enclosed individually in quotes.
We do have a REGEX parsing feature that can work with strings like this: https://toolset.com/documentation/user-guides/cred-conditional-display-engine/#functions
However, it's not possible to use a Types field shortcode in the expression so I can't compare against the membership-name field value. I'm afraid I don't have a very good solution available right away. What you need is a custom shortcode that parses the strings produced by the Types field shortcodes and the eMember shortcodes, then compares them in PHP and returns a 0 or 1 to indicate whether or not there is a match. Then register those shortcodes in Toolset > Settings, and our conditional HTML system will be able to interpret the values successfully. That level of custom code is beyond the scope of support we offer here in the forums. I might be able to work on something, but this task will go to the back of the queue since it requires a significant amount of custom code. Let me know if you would like me to do that.
If you need a faster solution, you may be able to connect with a professional developer in our contractors portal: https://toolset.com/contractors
First of all, thank you Christian for all your help.
At the moment I don't have budget available to contact a professional developer and I would like if you could help me to find a solution.
Thank you
Christian I wanted to warn you that I found an alternative solution, so I don't need any more help.
I thank you once again for all the help you have provided.