Skip Navigation

[Resolved] Conditional output on page based on a User field value

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 2 voices.

Last updated by Waqar 4 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2671661

Hi
I need to show different outputs on page based on the user role AND a numeric custom user field called Max1. In the block editor the Conditional editor doesn't let me add the second condition, so I tried the followings with the advanced editor:

( ( '[wpv-current-user info="role"]' eq 'shop_manager' ) AND [types usermeta='max1' user_current='true' output='raw'][/types]' gt '0' ) or
AND ( [types usermeta='wpcf-max1' output='raw'][/types]' gt '0' ) ) or
AND ( [types field='max1' output='raw'][/types]' eq '0' ) )

but none of them are accepted. Please let me know the rigth syntax. I read other similar support threads and I noticed that you often suggest to add a Field&Texts and/or a snippet. I can accept any solution as long as I can use other blocks inside the conditional.
Thanks
Kind regards
Nicola

#2671873

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Nicola,

Thank you for contacting us and I'd be happy to assist.

From looking into the conditional statement, I couldn't fully understand the exact requirement for the conditional statement to work.

Can you please share some more details about the required conditional output scenario, so that I can suggest the correct syntax accordingly?

regards,
Waqar

#2671887

Hello Waquar,
maybe I wasn't clear on the conditional statement, sorry, they are actually three different statements that I have tried but none is working
( ( '[wpv-current-user info="role"]' eq 'shop_manager' ) AND [types usermeta='max1' user_current='true' output='raw'][/types]' gt '0' )
----------------------
( ( '[wpv-current-user info="role"]' eq 'shop_manager' ) AND ( [types usermeta='wpcf-max1' output='raw'][/types]' gt '0' ) )
---------------------
( ( '[wpv-current-user info="role"]' eq 'shop_manager' ) AND ( [types field='max1' output='raw'][/types]' gt '0' ) )
They are supposed to test condition to show output when user role is "shop manager" AND max1 field value is gt 0. None of them is accepted in the block conditional editor.

thanks

#2672621

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

On my test website, I was able to make the following conditional statement work from the advanced editor of the conditional block:


  ( ( '[wpv-current-user info="id"]' eq 'shop_manager' ) AND ( '[types usermeta=max1 output=raw current_user=true]' gt '0' ) ) 

#2672657

Hello Waqar,
the statement you suggested is accepted by the editor, but the condition doesn't actually work in any case. I have applied it on two different pages and the condtional content whatever it is is not shown, be max1 gt 0 or max1 eq 0. Whatever value max1 gets nothing is shown. I have set max1=1 and shown the value on the page using a Field&Text block and 1 is correctly displayed. Then I turned to the HTML editor in the block and it shows [types usermeta='max1' output='raw' current_user='true'] using 's for all parameters. If I copy this text in the condition it is not accepted by the editor, is this a bug ? how can I workaround this issue ?
thanks

#2673209

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back and it is strange that this is not working on your website.

Can you please share temporary admin login details, along with an example page where this conditional block is?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2673947

Hello Waqar
Please don't forget about this, I am stuck !
In the meantime I have found why the conditional wasn't accepted by the editor, the [/types] was missing. I have created a content template (aaa-test) that works fine on the page (you can se that It Works ! is shown) but it's written [types usermeta='max1' output='raw' current_user='true'] with the 's. If I add the 's to the test in the conditional block it's not accepted, while if I don't use the 's it's accepted, but it doesn't work. Is this a bug ?
I so I think I will work this around using a content template instead. Please let me know asap, thanks

#2674209

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

Just wanted to let you know that I'm still working on this and will share the findings, by the end of the day today.

Thank you for your patience.

#2674233

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

conditional-block-example.png

While, the workaround of including the conditional statement in a separate template sounds workable. Here is a simpler alternative.

You can register a custom shortcode, that will check the current user's role and the target custom field's value, and return 'yes' if the condition is met/true, and 'no' otherwise:


add_shortcode( 'custom_add_post_cap', 'custom_add_post_cap_func');
function custom_add_post_cap_func()
{
	$allowed = 'no';
	$target_role_slug = 'shop_manager';
	$target_field_slug = 'max1';

	// get current user
	$user = wp_get_current_user();

	if ( $user->ID != 0 ) {
		// if current user has the target role
		if ( in_array( $target_role_slug, $user->roles ) ) {
			// get the value from the target field value
			$target_field_value = types_render_usermeta( $target_field_slug, array( 'user_id' => $user->ID, 'output' => 'raw' ) );
			// if the target field's value is greater than or equal to 1
			if( $target_field_value >= 1 ) {
				// set the returned value to 'yes'
				$allowed = 'yes';
			}		
		}
	}
	return $allowed;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "custom_add_post_cap" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

After that, you'll be able to use this custom shortcode, in the conditional block's interface, as shown in the attached screenshot.

#2674677

Hello Waqar,
I appreciate your solution, but not being a developer I prefer to go for the Content Template solution that works fine too. I am surprised that the conditional block editor doesn't accept the syntax that works fine with the classic editor, please raise a warning to your developers.
Thanks
Regards
Nicola

#2674679

Hello Waqar,
I appreciate your solution, but not being a developer I prefer to go for the Content Template solution that works fine too. I am surprised that the conditional block editor doesn't accept the syntax that works fine with the classic editor, please raise a warning to your developers.
Thanks
Regards
Nicola

nicolaS-3 confirmed that the issue was resolved on 2023-12-19 11:27:13.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.