• Resolved jim-de-groot

    (@jim-de-groot)


    Hi,

    i’ve searching on the forum for a way to check a user role or level, but i haven’t managed to find a solution for my problem yet.
    I want to have page with an overview of the blog’s authors, but I want to seperate them based on their role or level. So at the top of the page the authors with admin role, next comes the editors, and last the regular authors/bloggers.

    I’ve been trying to do it with if($curauth->user_level == x) but that wasn’t working. I think the user_level isn’t available in wp2.7.

    Can anyone please help me? Thanks!

Viewing 15 replies - 1 through 15 (of 18 total)
  • Use wp_list_authors()

    Thread Starter jim-de-groot

    (@jim-de-groot)

    How can I seperate on different roles with wp_list_authors()?
    Does it have an argument for roles or levels?

    Try this:

    <?php
    //list each role and each user with that role
    global $wp_roles;
    foreach( $wp_roles->role_names as $role => $name ) {
      $name = translate_with_context($name);
      echo '<p>List of users in the role '.$role .' ('. $name . '):</p>';
      $this_role = "'[[:<:]]".$role."[[:>:]]'";
      $query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
      $users_of_this_role = $wpdb->get_results($query);
      if ($users_of_this_role) {
        foreach($users_of_this_role as $user) {
          $curuser = get_userdata($user->ID);
          $author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
          echo '<p>--User nicename: '.$curuser->user_nicename .', display Name: '. $curuser->display_name . ', link to author posts <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->user_nicename .'</a></p>';
        }
      }
    }
    ?>

    Thread Starter jim-de-groot

    (@jim-de-groot)

    Thanks Michael, with your help I could make it work the way I wanted.

    Thread Starter jim-de-groot

    (@jim-de-groot)

    Yesterday I thought I had the right solution, but now I discovered a new problem. It works in Firefox but not in Internet Explorer.
    In IE there is this php error:
    “Warning: Invalid argument supplied for foreach()” for the line:
    foreach( $wp_roles->role_names as $role => $name )

    Can you help me again MichaelH?

    That’t not because you’re using IE, but because you’re not logged in ->

    $wp_roles is empty. I know because I’ve tried this before and I had to resort to hardcoding the roles:

    $roles = array(‘admin’, ‘editor’, etc.);

    foreach( $roles as $role ) …

    Thread Starter jim-de-groot

    (@jim-de-groot)

    Thanks for your quick reaction scribu.

    I’m new to wp and php, so i’m not familiar with the functions and variables inside the code example.
    I’m not sure how to apply your suggestion to my code, because I want to test on the roles in their Dutch translated names like this:

    <?php
    
    /* Loop through users to search for the admin and editor users. */
    //list each role and each user with that role
    global $wp_roles;
    foreach( $wp_roles->role_names as $role => $name )
    {
      	$name = translate_with_context($name);
      	if($name == 'Beheerder' || $name == 'Bewerker')
      	{
    	  	//echo '+Users in the role '.$role .' ('. $name . '):';
    	  	$this_role = "'[[:<:]]".$role."[[:>:]]'";
    	  	$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
    	  	$users_of_this_role = $wpdb->get_results($query);
    	  	if ($users_of_this_role)
    		{
    	    	foreach($users_of_this_role as $user)
    			{
    		    	$curuser = get_userdata($user->ID);
    		      	$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
    				echo "<div class='post'>";
    					echo "<a href='.$user_link.' title='.$curuser->display_name.'>";
    						echo "<h2>$curuser->display_name</h2>";
    					echo '</a>';
    					echo "<a href='.$user_link.' title='.$curuser->display_name'>";
    						echo get_avatar($curuser->user_email, '80', $avatar);
    					echo '</a>';
    					echo ''.$curuser->description.'';
    				echo '</div>';
    		    }
    	  	}
    	}
    }
    ?>

    How should I use this array to get the same functionality?
    $roles = array('Beheerder', 'Bewerker', 'Auteur', 'Abonnee');
    “Beheerder” is an admin
    “Bewerker” is an editor
    “Auteur” is an author
    “Abonnee” is a subscriber

    Thread Starter jim-de-groot

    (@jim-de-groot)

    I’ve done it now with this code:

    $roles = array('administrator', 'editor', 'author', 'contributor', 'subscriber');
    
    /* Loop through users to search for the admin and editor users. */
    foreach( $roles as $role )
    {
      	// all users with admin or editor role
    	if($role == 'administrator' || $role == 'editor')
      	{
    	  	$this_role = "'[[:<:]]".$role."[[:>:]]'";
    	  	$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
    	  	$users_of_this_role = $wpdb->get_results($query);
    	  	if ($users_of_this_role)
    		{
    	    	foreach($users_of_this_role as $user)
    			{
    		    	$curuser = get_userdata($user->ID);
    		      	$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
    				echo "<div class='post'>";
    					echo "<a href='.$user_link.' title='.$curuser->display_name.'>";
    						echo "<h2>$curuser->display_name</h2>";
    					echo '</a>';
    					echo "<a href='.$user_link.' title='.$curuser->display_name'>";
    						echo get_avatar($curuser->user_email, '80', $avatar);
    					echo '</a>';
    					echo '<p>'.$curuser->description.'</p>';
    				echo '</div>';
    		    }
    	  	}
    	}
    }
    ?>

    I stoppped matching on $name, but used $role instead. That way I won’t have to use the translation.

    Glad to see you figured it out.

    is there a way to make that code so it shows in pages? i have over 300 member and would rather show like 25 or 50 to a page..

    Thread Starter jim-de-groot

    (@jim-de-groot)

    Maybe you can add some extra constraints to decrease the number of members to show. For example test on the number of submitted posts or something like that.

    For paging you could look how its done for archive pages. Maybe you could figure out how it is done there with wp_pagenavi. I’m not sure if it is build in for archive pages or if it is part of the WP-PageNavi plugin I am using.

    In order to save space i made it so it would only show the members avatar, now just to find a way to may a tooltip show on the avatar and ill be ok haha, you can view here

    https://jericosystems.com/members/

    Hi, this is exactly what I need but when I tried scribu’s script this is what I get (no users listed)

    List of users in the role administrator (Administrator):
    List of users in the role editor (Editor):
    List of users in the role author (Author):
    List of users in the role contributor (Contributor):
    List of users in the role subscriber (Subscriber):

    and Jim de Groot’s code produces an empty page. What am I doing wrong? thanks for your answer in advance!

    Have you tried MichaelH’s version?

    Turn on WP_DEBUG in wp-config.php and see if you get any errors on that page.

    Yes, sorry, when I wrote “scribu’s” script I actually thought of MichelH’s script. It produces an OK list of user roles as shown above, but no users are displayed, although there are users in some categories, obviously. WP_DEBUG did not give any clues.. thanks, scribu!

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘display info of user with certain role or level ini WP 2.7’ is closed to new replies.