WPFront User Role Editor Shortcode

Below, you’ll find a detailed guide on how to add the WPFront User Role Editor Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WPFront User Role Editor Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the WPFront User Role Editor Plugin and the shortcodes it provides:

Plugin Icon
WPFront User Role Editor

"WPFront User Role Editor is a powerful WordPress plugin that grants you the ability to easily modify user roles. Manage permissions and customize user capabilities with ease."

★★★★✩ (64) Active Installs: 50000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [CURRENT_USER_ROLES]

WPFront User Role Editor [CURRENT_USER_ROLES] Shortcode

The wpfront-user-role-editor shortcode provides a list of the current user’s roles. If the user is not logged in, it returns an empty string. If logged in, it retrieves the user’s roles and returns them as a comma-separated list. If the user has no roles, it returns ‘None’.

Shortcode: [CURRENT_USER_ROLES]

Examples and Usage

Basic example – Displaying the roles of the current user

[current_user_roles /]

PHP Function Code

In case you have difficulties debugging what causing issues with [CURRENT_USER_ROLES] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode(self::CURRENT_USER_ROLES, array($this, 'process_current_user_roles'));

Shortcode PHP function:

function process_current_user_roles($atts, $content, $shortcode) {
            if(!is_user_logged_in()) {
                return '';
            }
            
            $atts = shortcode_atts(array('label' => __('Current Roles: ', 'wpfront-user-role-editor')), $atts, $shortcode);
            $label = $atts['label'];
            
            $user = wp_get_current_user();
            if(empty($user->roles)) {
                $roles_text = __('None', 'wpfront-user-role-editor');
            } else {
                $roles = $user->roles;
                global $wp_roles;
                $role_names = $wp_roles->role_names;
                
                $names = array();
                foreach ($roles as $r) {
                    if(!empty($role_names[$r])) {
                        $names[] = $role_names[$r];
                    }
                }
                
                $roles_text = implode(', ', $names);
            }
            
            return $label . $roles_text;
        }

Code file location:

wpfront-user-role-editor/wpfront-user-role-editor/includes/shortcodes/class-shortcodes.php

Conclusion

Now that you’ve learned how to embed the WPFront User Role Editor Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *