Skip Navigation

[Resolved] Conditional logged_in and user role

This support ticket is created 2 years, 1 month 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 3 replies, has 2 voices.

Last updated by davidS-72 2 years ago.

Assisted by: Waqar.

Author
Posts
#2484811

Tell us what you are trying to do?

I want to show a text "Your user don't have permision" when a user is logged_in and has the role "pot_publicar_ofertes_de_feina".

This code show the text "Your user don't have permision" when the user role is "pot_publicar_ofertes_de_feina" and I don't know why.

[wpv-conditional if="( '[wpv-current-user info='logged_in']' eq 'true' ) AND ( ' [wpv-current-user info='role']' ne 'pot_publicar_ofertes_de_feina' )"]
Your user don't have permision
[/wpv-conditional]

thanks
David

#2485907

Hi David,

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

To troubleshoot this, I'll need to see how these related elements are set up on the website.

Can you please share temporary admin login details, along with the link to the page with this condition?

Also, include the username and password for one user with the "pot_publicar_ofertes_de_feina" user role, that you're testing with.

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

regards,
Waqar

#2491295

Thank you for sharing these details.

I noticed that on your website users have multiple user role assigned to them, whereas the shortcode "wpv-current-user" can only return a single user role.

For this reason, you'll need to register a custom shortcode, that can check for multiple user roles assigned to the currently logged-in user and return 'yes' if the target user role is found and 'no' if not:


add_shortcode( 'custom_check_user_role', 'custom_check_user_role_func');
function custom_check_user_role_func($atts)
{
	$a = shortcode_atts( array(
		'role' => ''
	), $atts );

	$found = 'no';

	$user = wp_get_current_user();

	if ( $user->ID != 0 ) {
		if ( in_array( $a['role'], $user->roles ) ) {
			$found = 'yes';
		}
	}
	return $found;
}

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_check_user_role" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

To use the shortcode, you just have to pass on the target user role to look for in the shortcode attribute "role", for example:


[custom_check_user_role role='pot_publicar_ofertes_de_feina']

And the same shortcode can be used in the conditional statement like this:


[wpv-conditional if="( '[custom_check_user_role role='pot_publicar_ofertes_de_feina']' eq 'yes' )"]

// show something if the current user has the 'pot_publicar_ofertes_de_feina' role

[/wpv-conditional]

And the combined statements for the 'pot_publicar_ofertes_de_feina' or the 'administrator' roles would look like this


[wpv-conditional if="( '[custom_check_user_role role='pot_publicar_ofertes_de_feina']' eq 'yes' ) OR ( '[custom_check_user_role role='administrator']' eq 'yes' )"]

// show something if the current user has either the 'pot_publicar_ofertes_de_feina' or the 'administrator' role

[/wpv-conditional]

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

#2491433

My issue is resolved now. Thank you!