WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been modifying a long abandoned plugin project that provides a Stackoverflow inspired badge system for Wordpress. I have upgraded some of its code so it functions with the new Wordpress version.

Please view the plugin code here: http://pastebin.com/kCWWLPL2

The problem is that the original code did not provide a way to echo the badges the user have been awarded. Currently, there is a way of displaying the badges: <?php rhb_list_badges() ?> this function will list every single badge in the system.

I want to modify the code so that by adding a function to the author.php page of Wordpress, it will list the user's awarded badges. However, my knowledge of PHP is very limited and I have hit a wall.

I was told that this might work:

$user_id = get_current_user_id();
if ($user_id != 0) {
  rhb_list_badges(array('user_ID' => $user_id));

But all it did was to list the all the badges and not just the awarded ones for the user. Maybe something is missing in the above code? I am certain that there must be a way to accomplish this. Do you know how?

Update: If we look in the original code, we can see that there is a filter already in place:

function rhb_get_badges( $filter = '' ) {

        global $wpdb;

        if ( empty($filter ) ) { $filter = array(); }

        // Select all rows by default
        $sql = 'SELECT badge_id, name, description, type FROM ' . $wpdb->prefix . 'rh_badges b WHERE 1=1 ';

        // If a user ID was entered.
        if ( array_key_exists('user_ID', $filter) ) {

                $user_ID = $filter['user_ID'];

                // Join the rh_user_badges table.
                $sql = 'SELECT b.badge_id, b.name, b.description, b.type
                                FROM ' . $wpdb->prefix . 'rh_badges b,
                                        ' . $wpdb->prefix . 'rh_user_badges ub
                                WHERE b.badge_id = ub.badge_id
                                AND ub.user_id = ' . $user_ID;


        // If a badge ID was entered.
        if ( array_key_exists('badge_ID', $filter) ) {

                $badge_ID = $filter['badge_ID'];

                // Append a WHERE clause to the SQL.
                $sql .= " AND b.badge_id = $badge_ID";

        $badges = $wpdb->get_results( $sql );

        return $badges;

function rhb_list_badges( $filter = '' ) {

        if ( empty($filter ) ) { $filter = array(); }

        print '';

                        foreach (rhb_get_badges( $filter ) as $badge) {

                        print '<div class="biobadge"><div class="';

                                        if ( 'gold' == $badge->type )
                                                echo 'bgold';
                                        elseif ( 'silver' == $badge->type )
                                                echo 'bsilver';
                                        elseif ( 'bronze' == $badge->type )
                                                echo 'bbronze';

                                        print '"></div>
                                                ' . $badge->name . '

How would I go about to use it?

share|improve this question
up vote 0 down vote accepted

get_current_user_id(); gets the current logged in user id and you need the user whos profileUrl is views so try this insted to get the right id:

$author = get_user_by( 'slug', get_query_var( 'author_name' ) ); 
echo $author->ID;'

yep, so instead of your code try this:

$author = get_user_by( 'slug', get_query_var( 'author_name' ) ); 
if ($author->ID > 0) {
  rhb_list_badges(array('user_ID' => $author->ID));
share|improve this answer
Can you please incorporate your code example with mine and I can run it to see if it works? – Amanda Bynes Aug 23 '12 at 23:44
Further guidance and a code example to run would be ideal, cheers for responding. – Amanda Bynes Aug 23 '12 at 23:50
i updated my answer – Bainternet Aug 24 '12 at 8:43
I tried it but it did not return anything I am afraid. However, I have updated my question if you could please look. The original code already had a filter system in place, if you could kindly look through that update and see if you know how I could use it? – Amanda Bynes Aug 24 '12 at 12:32
Forget that. I just made further testings and you were correct, the original code returned the user that is online's badges. How can I change it so that it displays the author's? Maybe there is a typo in your code? – Amanda Bynes Aug 24 '12 at 12:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.