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
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.
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
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
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;
}
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
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.