Skip Navigation

[Resolved] If Conditional not working for custom User Role & User ID

This support ticket is created 5 years, 12 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
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)

This topic contains 7 replies, has 2 voices.

Last updated by Nashaat 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1146541

I am trying to use the conditional output for custom shortcodes

this code gets the user role:


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

This code gets user ID for archive page by slug

function get_user_id() {
 $author = get_user_by( 'slug', get_query_var( 'author_name' ) );
 return $author->ID;
}
add_shortcode( 'get_user_id', 'get_user_id' );

now i am creating an archive page for author and would like to show information of each Author

for example this will show the login name of each author depending on the slug author/use1 ...author/user2 etc...

[wpv-user field="user_login" id="[get_user_id]

now i managed also to show the role of the author with following code

[get_user_role userid="[get_user_id]"]

but the issue comes when i try to use the conditional output like following:


[wpv-conditional if="( '[get_user_role userid='[get_user_id]' eq 'administrator' )"]
I AM ADMIN
[/wpv-conditional]

with this code it looks like i dont get results.

#1146624

First, be sure you add get_user_role and get_user_id in Toolset > Settings > Frontend Content > Third party shortcodes, as well as Toolset > Settings > Frontend Content > Functions inside conditional evaluations.

If that doesn't solve the problem, add debug="true" to the conditional shortcode and check the results on the front end of the site.

[wpv-conditional if="( '[get_user_role userid='[get_user_id]' eq 'administrator' )" debug="true"]
I AM ADMIN
[/wpv-conditional]

You should see some extra information written to the screen describing the conditional logic. Please copy + paste that here for me to review. If you cannot see this information, it could be a problem with too many nested shortcodes or nested quotation marks. If that is the case, let me know and we can discuss other options.

#1146722
shortcodes-registered.png
broken shortcodes.png

Thanks for your reply Christian.

I have the shortcodes registered in Third Party .. and now i have registered them in Functions inside conditional evaluations.
since i registered them i began to see such broken shortcodes from other usual Toolset shortcodes (image broken shortcodes)

I deleted everything from the template for author archive and kept only the conditional output with debug='true'. no debugging information showed up.

#1146738
Users-loop-views.png

I have tried to add the conditional output to a users view.. also didn't show up anything.

#1146745

Okay the next thing to try is adjusting the nested quotation marks. Try this alternate syntax instead, where you alternate nesting double and single quotes:

[wpv-conditional if="( '[get_user_role userid="[get_user_id]" eq 'administrator' )" debug="true"]

If the debug information still doesn't appear, it means that you have exceeded the maximum level of nested shortcode attributes in the wpv-conditional clause. In that case, you must write one custom shortcode that combines the two nested shortcodes. In other words, you need one shortcode with no shortcode attributes. It must return the role of the current archive author.

#1146749

Yes i think you are right no debugging information. so i tried following code and it seems to work

function get_user_id_archive( $atts )
{
  $a = shortcode_atts( array(
      'userid' => ''
  ), $atts );
  $userdata = get_user_by( 'slug', get_query_var( 'author_name' ) );
  $user_roles = isset($userdata->roles) ? $userdata->roles : array();
  $user_role = array_shift($user_roles);
  return $user_role;
}
add_shortcode( 'get_user_role_archive', 'get_user_id_archive' );

conditional output

[wpv-conditional if="( '[get_user_role_archive]' eq 'administrator' )"]
I AM ADMIN
[/wpv-conditional]

it works without registering the shortocde and function in toolset Third-party shortcode arguments &
Functions inside conditional evaluations. is that ok or better to register?

please let me know if i can improve the above solution. THANK YOU VERY MUCH

#1147281

Looks good to me. I would register the shortcode in third-party shortcode arguments if you plan to use it anywhere else, like as the value of any other shortcode attribute. If you don't plan to use it anywhere else, you probably do not need to register it. For example, let's say you have custom fields with slugs that match User roles, and you want to access the corresponding field on the author's archive:

[types field="[get_user_role_archive]"][/types]

In this case, you would need to register the shortcode in third-party shortcode arguments.

#1148561

Thank you very much Christian!