Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance [gvlogic] by adding user role check #2234

Open
rafaehlers opened this issue Dec 11, 2024 · 0 comments
Open

Enhance [gvlogic] by adding user role check #2234

rafaehlers opened this issue Dec 11, 2024 · 0 comments

Comments

@rafaehlers
Copy link
Contributor

rafaehlers commented Dec 11, 2024

R. D. shared this code with us:

/**
 * Replace {current_user:role_is:[...]} Merge Tag
 * 
 * @param string $text Text to be parsed for Merge Tag.
 * 
 * @return string
 */
add_filter( 'gform_replace_merge_tags', function ( $text ) {

	preg_match( '#{current_user:role_is:(.*?)}#', $text, $matches );

	if ( empty( $matches ) ) {
		return $text;
	}

	$roles = explode( '||', $matches[1] );
	
	if ( array_intersect( wp_get_current_user()->roles, $roles ) ) {
		return str_replace( $matches[0], 'true', $text );
	}

	return str_replace( $matches[0], 'false', $text );
}, 1, 1 );

You need to use it like so:

[gvlogic if='{current_user:role_is:administrator}']is an admin[else]not an admin[/gvlogic]

or

[gvlogic if='{current_user:role_is:admin||meeting-participant}']welcome[/gvlogic]`

We only have this way of filtering user roles at the moment: https://docs.gravitykit.com/article/447-filtering-entries-by-wordpress-user-roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant