Skip Navigation

[Resolved] How to use user meta fields in conditional shortcodes

This thread is resolved. Here is a description of the problem and solution.

Problem:
The UI doesn't let you insert user meta fields when using the conditional output button to insert the wpv-conditional fields.

Solution:
You need to insert them manually. The correct format to test whether a user meta checkboxes field is empty (the use-case for the client) is like so:

[wpv-conditional if="( '[types usermeta='prediction' current_user='true' output='raw'][/types]' ne '' )"]
<p>What to display if condition met</p>
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Nigel 6 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1116163

Hi the code is below

[wpv-conditional if="( '[types usermeta='your-roles'][/types]' eq '' )"]
pls update
[/wpv-conditional]

 [wpv-conditional if="( '[types usermeta='your-roles'][/types]' ne '' )"]          
  <span class="ep_roles">
[types usermeta='your-roles' separator="</span><span class='ep_roles'>" current_user="true"][/types]</span>
  [/wpv-conditional] 

the first part suppose to output "pls update " but its not .
second part output fine .

pls advice if you see any mistake in the code. thanks.

#1116310

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Is it just that you are missing the parameter current_user="true" in your first example?

It seems to work okay on a test site of mine.

#1116494

Hi, The field is user form checkboxes field.

the first part is sstill not working well.

[wpv-conditional if="( '[types usermeta='your-roles current_user='true'][/types]' eq '' )"]
pls update
[/wpv-conditional]

The field is user form checkboxes field with 4 checkbox options.

#1116703

from here : https://toolset.com/forums/topic/conditional-display-with-multi-checkboxes-field-type/#post-746505

Minesh said : Checkboxes fields are not supported for conditional post field display.

is this still the same case?

#1116864
[types usermeta='how-did-you-hear-about-us' current_user='true' state="checked" option="0"]Banners[/types]
[types usermeta='how-did-you-hear-about-us' current_user='true' state="checked" option="1"] Media [/types][types usermeta='how-did-you-hear-about-us' current_user='true' state="checked" option="2"]Human [/types]
[types usermeta='how-did-you-hear-about-us' current_user='true' state="checked" option="3"] ads[/types]
[types usermeta='how-did-you-hear-about-us' current_user='true' state="checked" option="4"]Youtube  [/types]

Hi,. the above code works , BUT how do i put conditional if nothing[none selected ] is selected output " no result" ? How to merget them all. ?

#1117097

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I did some testing and found that it doesn't appear to work, even though in our documentation it says it should: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value

I'm reporting that so that it can be investigated. It should either work, or the documentation should be changed.

In the meantime as a workaround I crafted a simple custom function you can use in your conditional shortcodes.

<?php
/**
 * Function to test if user checkboxes field set
 */
function user_condition( $field ){

	$userid = get_current_user_id();

	$return = "";

	if ( isset( $field ) ) {

		$value = get_user_meta( $userid, $field, true );

		if ( empty( $value ) ) {

			$return = "empty";
		}
	}

	return $return;
}

You will need to register the function user_condition at Toolset > Settings > Front-end content to be able to use it in the conditional shortcode.

You pass the field slug (with the 'wpcf-' prefix if it is a Types field), like so:

[wpv-conditional if="( user_condition( 'wpcf-prediction' ) eq 'empty' )"]
User condition says empty!
[/wpv-conditional]

So, if no checkboxes are checked the function will return the string 'empty'. You can either test with 'eq' or 'ne' as it suits.

#1117275

hi, this shortcodes has some issue , is the code closed well for a shortcode snippets ?

pls raise this issue of the checkboxes issue to the 2nd tier while we try to find a solution. thanks.

#1117544

ok, after some testing i understand that this fn is only to detect if any checkboxes fields is checked. thanks.

#1118155

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I heard back from my colleague who pointed out that where I had reported it not working I had a simple typo in the shortcode. I double checked and they were right.

So, testing once more, I found that the following worked:

[wpv-conditional if="( '[types usermeta='prediction' current_user='true' output='raw'][/types]' ne '' )"]
<p>Prediction: [types usermeta='prediction' current_user='true'][/types]</p>
[/wpv-conditional]

There I am testing whether any one of the options of a checkboxes user field is checked. I tested inserting it directly in a page, and inside a Form.