Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active October 20, 2023 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackkatz/9bd4845ee56eb9df4b53703fe249fd9f to your computer and use it in GitHub Desktop.
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
<?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