Skip Navigation

[Resolved] Problem Using Conditional Logic in Template

This support ticket is created 4 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
- 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 6 replies, has 2 voices.

Last updated by michaelO-8 4 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1473847

Tell us what you are trying to do?

I am trying to make changes to conditional logic in a template on the dev site. After making changes, the conditional logic no longer seems to work, even after changing the code back to how it was.

The logic that no longer works is:
[wpv-if evaluate="'[wpv-current-user info='id']' = '[wpv-post-author format='meta' meta='ID']' OR '[wpv-current-user info='role']' = 'administrator'"]

After reading the documentation, I did notice that the syntax seems to differ from the documentation, for example `=` seems to be written as `eq`, so I am not sure if this conditional logic is old (or even valid?), but it did used to work before I tried to save the template (it still works on the live site). I would like to get this working properly on the dev site before trying to make changes on the live site.

What is the correct way to evaluate if the user is the post author or the administrator role?

Is there any documentation that you are following?

https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/
Is there a similar example that we can see?

What is the link to your site?

hidden link
However, the conditional logic is only supposed to display the buttons I am trying to display if you are the post author or an admin.

#1474499

Waqar
Supporter

Languages: English (English )

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

Hi Michael,

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

Looking at the conditional block that you've shared, it seems some of the text was lost during the changes.

The complete shortcode block for this case would be:


[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-current-user info='id']' ) OR ( '[wpv-current-user info='role']' eq 'administrator' )"]
Show this only if the logged-in user is the post author or has administrator role
[/wpv-conditional]

The following documentation links will help you better understand its usage:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-conditional
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/nesting-and-combining-conditions/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1475099

Hi Waqar,

Thank you for your help. The good news is that this allows the post author to view the content within the block, but for some reason does not allow the administrator to view it.

The odd thing is that, after realizing this, I tested the old code again and the old code actually still allows the post author to view the content within the block, but also is not working for the admin.

Before I tried to make any changes to the template, the admin was able to view the content within the block. Any idea what could be the problem?

#1476073

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing the update.

It is strange, that a user with the administrator role is not able to access the content in the conditional block. The same block works as expected on my test website.

I'll recommend making sure that WordPress, active theme and all plugins are updated to their latest versions.

In case the issue still persists, you can include the debug="true" attribute in the conditional block's shortcode and share that debug information with me.
( ref: https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/#debugging-conditional-output-in-views )

Example:


[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-current-user info='id']' ) OR ( '[wpv-current-user info='role']' eq 'administrator' )" debug="true"]
Show this only if the logged-in user is the post author or has administrator role
[/wpv-conditional]

To expedite this troubleshooting, you can also share temporary admin login details. I'll need to see exactly where and how this block is being used in the admin area and which user (with an "administrator" role) is being used for this testing.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#1480225

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing the admin access.

The main issue on your website is that the shortcode [wpv-current-user info='role'] is not returning the role of the current user with administrator role.
( it seems to be working for other roles for example "candidate" )

To troubleshoot this, I'll recommend testing this will all non-Toolset plugins disabled and a default theme like Twenty Twenty.

If the issue is fixed, you can start adding the disabled items, one-by-one, to narrow down to a possible conflicting one.

In case it still persists, I'll need a clone or snapshot of your website, to investigate this on my own server, without affecting the actual website.
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

Note: I've set your next reply as private.

#1484583

Waqar
Supporter

Languages: English (English )

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

Thank you for waiting while I performed some troubleshooting on your website's clone.

I noticed that the issue seems specific to few users, for which the user meta entry for key "wp_9xbrh8d4d7_capabilities" is saved differently, in the "wp_9xbrh8d4d7_usermeta" table.

For example, for the user "temporaryadmin" with user ID "2124", the capability metadata is saved as:

	
a:14:{s:26:"wpcf_custom_post_type_view";b:1;s:26:"wpcf_custom_post_type_edit";b:1;s:33:"wpcf_custom_post_type_edit_others";b:1;s:25:"wpcf_custom_taxonomy_view";b:1;s:25:"wpcf_custom_taxonomy_edit";b:1;s:32:"wpcf_custom_taxonomy_edit_others";b:1;s:22:"wpcf_custom_field_view";b:1;s:22:"wpcf_custom_field_edit";b:1;s:29:"wpcf_custom_field_edit_others";b:1;s:25:"wpcf_user_meta_field_view";b:1;s:25:"wpcf_user_meta_field_edit";b:1;s:32:"wpcf_user_meta_field_edit_others";b:1;s:13:"administrator";b:1;s:13:"bbp_keymaster";b:1;}

Usually, the user role information is the first item in this serialized data, like this:

	
a:14:{s:13:"administrator";b:1;s:13:"bbp_keymaster";b:1;s:26:"wpcf_custom_post_type_view";b:1;s:26:"wpcf_custom_post_type_edit";b:1;s:33:"wpcf_custom_post_type_edit_others";b:1;s:25:"wpcf_custom_taxonomy_view";b:1;s:25:"wpcf_custom_taxonomy_edit";b:1;s:32:"wpcf_custom_taxonomy_edit_others";b:1;s:22:"wpcf_custom_field_view";b:1;s:22:"wpcf_custom_field_edit";b:1;s:29:"wpcf_custom_field_edit_others";b:1;s:25:"wpcf_user_meta_field_view";b:1;s:25:"wpcf_user_meta_field_edit";b:1;s:32:"wpcf_user_meta_field_edit_others";b:1;}

For all such users which don't have the user role information, saved as the first item, the [wpv-current-user info='role'] shortcode doesn't work.

These findings have already been shared with the concerned team for further review and I'll keep you updated on the progress through this ticket.

For now, a workaround can be to register a custom shortcode to get the user role, that uses a different approach:


function get_user_role_func( $atts )
{
  $a = shortcode_atts( array(
      'userid' => ''
  ), $atts );
  $userdata = get_user_by('ID', $a['userid']);
  $user_roles = $userdata->roles;
  $user_role = array_shift($user_roles);
  return $user_role;
}
add_shortcode( 'get_user_role', 'get_user_role_func' );

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 active theme's "functions.php" file.

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

Using this new shortcode, you'll be able to get the user's role like this:


[get_user_role userid="[wpv-current-user info='id']"]

And the same can be used in the conditional block as:


[wpv-conditional if="( '[wpv-post-author format='meta' meta='ID']' eq '[wpv-current-user info='id']' ) OR ( '[get_user_role userid="[wpv-current-user info='id']"]' eq 'administrator' )"]
Show this only if the logged-in user is the post author or has administrator role
[/wpv-conditional]

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1484919

Thank you for the help! I will give this a try and let you know if I have any issues.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.