/** * Adding the following to your functions.php file will give you a custom * shortcode to display a user's role. The shortcode can be used as follows: * * [display_user_role] -> this would display the current user's role. * * [display_user_role id=123] -> displays the role for user ID 123. */ add_shortcode( 'display_user_role', function( $atts, $content, $tag ) { global $wp_roles; $user_id = ( isset( $atts['id'] ) ) ? $atts['id'] : get_current_user_id(); $roles = wpmem_get_user_role( $user_id ); if ( is_array( $roles ) ) { foreach ( $roles as $role ) { $display[] = translate_user_role( $wp_roles->roles[ $role ]['name'] ); } $content = implode( ", ", $display ); } else { $content = translate_user_role( $wp_roles->roles[ $roles ]['name'] ); } return $content; }, 10, 3);
Not sure what to do with this code?
You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.
Here are some free articles to get you started:
- Using Code Snippets from the Site
- The functions.php File
- Create a plugin file for custom functions
- Create a child theme
- Do not modify plugin files!
For "hands on" help, consider a plugin support subscription or the Pro Bundle.