• Resolved todd402

    (@todd402)


    Hi,

    I’ve seen this post https://www.ads-software.com/support/topic/show-total-number-of-registered-users-by-user-role/ which gives totals of specific roles via shortcode. Is it possible someway to combine several roles to be included in a shortcode? In the example linked above, the shortcode [um_total_members role=”subscriber”] shows the total for that one role. I’d like to be able to have something like [um_total_members role=”subscriber” & role=”contributor”] to show the total number of members for both roles combined into one shortcode. Is anyone able to help me achieve this please?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support andrewshu

    (@andrewshu)

    Hello @todd402

    Sorry, but customizations are not included in our support.

    Regards.

    @todd402

    You can try this shortcode.
    The parameter roles enter all your role IDs comma separated for the total count.

    [total_members_roles roles="role_id1,role_id2,role_id3"]

    add_shortcode( 'total_members_roles', function( $atts ) {
        
        global $wp_roles;
    
        $count_users = count_users();
        if ( ! empty( $atts['roles'] ) && isset( $count_users['avail_roles'] )) {
    
            $roles = array_map( 'trim', array_map( 'sanitize_text_field', explode( ',', $atts['roles'] )));
            $counter = 0;
            foreach( $roles as $role ) {
                if ( array_key_exists( $role, $wp_roles->roles ) && isset( $count_users['avail_roles'][$role] )) {
                    $counter += $count_users['avail_roles'][$role];
                }
            }
            return $counter;
        }
        return $count_users['total_users'];
    });

    Install the code snippet into your active theme’s functions.php file
    or use the “Code Snippets” plugin.

    https://www.ads-software.com/plugins/code-snippets/

    • This reply was modified 9 months, 2 weeks ago by missveronica.
    Thread Starter todd402

    (@todd402)

    @missveronicatv once again you have helped enormously, thank you. Your solution worked perfectly.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Count user roles and combine into total to display on page’ is closed to new replies.