Improving a Stackoverflow “inspired” badge system to display badges in author page

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:

<?php
$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 . '
                                                </div>';
                        }
                        ;
}

How would I go about to use it?

1 Answer
1

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));
}

Leave a Comment