Last active
October 20, 2023 00:14
-
-
Save zackkatz/9bd4845ee56eb9df4b53703fe249fd9f to your computer and use it in GitHub Desktop.
GravityView - Wrap entries in a custom CSS class containing the user role of entry creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds a CSS class for each user role. | |
* | |
* @param string $class The existing class. | |
* @param \GV\Template_Context The context. | |
* | |
* @return string The modified class, which will be sanitized by {@see gravityview_sanitize_html_class()} after return. | |
*/ | |
add_filter( 'gravityview/template/list/entry/class', function( $css_class = '', $gravityview ) { | |
$entry = $gravityview->entry->as_entry(); | |
$user = new WP_User( rgar( $entry, 'created_by' ) ); | |
// There was an error finding the user. | |
if ( ! $user->exists() ) { | |
return $css_class; | |
} | |
$extra_css_class = ''; | |
// Add a CSS class for each user role. | |
foreach( $user->roles as $role ) { | |
$extra_css_class .= ' gv-user-role-' . $role; | |
} | |
return $css_class . ' ' . $extra_css_class; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment