[resolved] Display author and users Badges within worpress/buddypress profile (20 posts)

  1. TheBigAdmin1
    Posted 3 years ago #

    First I would like to say that BadgeOS is a great plugin. I am looking for a solution to display individual badges on each author/users buddypress and author profile.

    I am a site admin for http://SportsUnbiased.com and we could really use some knowledge on enhancing the look and feel for our users.


  2. Michael Beckwith
    Posted 3 years ago #

    This isn't tested with a wide variety of badge data, so do try it on a dev server first. It's also in straight php code and not a shortcode or anything. If you are unfamiliar with any parts of it, especially the WP_Query part, reply and I'll answer questions.

    $current_user = wp_get_current_user();
    $ids = badgeos_get_user_earned_achievement_ids(
    $current_user->data->ID );
    $types = array();
    foreach( $ids as $id ) :
    	$types[] = get_post_type( $id );
    $args = array(
    	'post_type' => array_unique($types),
    	'post__in' => $ids
    $badges = new WP_Query( $args );
    if( $badges->have_posts() ) : while( $badges->have_posts() ) : $badges->the_post(); ?>
    <?php endwhile;
  3. TheBigAdmin1
    Posted 3 years ago #

    Where would this code go, in the author.php? I might need some more guidance.

  4. Michael Beckwith
    Posted 3 years ago #

    This wold go wherever you need to display the badges. Before I hand over some revised code, are you comfortable with templating and using the WordPress Loop? I want to make sure before I give you too much to handle. If you're not, then just let me know what parts you want to display for the current user being shown, and I can revise as necessary.

  5. TheBigAdmin1
    Posted 3 years ago #

    Id like to think I am proficient with wordpress but I am learning new things everyday. Id say i am about 90% comfortable templating but id like as much help as i can get on this particular issue.

    Id like to display up to 12 badges per author/user profile (badges resized to 75 x 75)
    - possible pagination to display next page of badges
    - badges listed in the top-right of the author/member profile

    I really appreciate your help.. really means alot.

  6. Michael Beckwith
    Posted 3 years ago #

    What sort of the details for the badges? Just the badge image and the name?

  7. TheBigAdmin1
    Posted 3 years ago #

    Yes, just the badge image and the name.. not sure if we need it to do anything else.

  8. Michael Beckwith
    Posted 3 years ago #

    Give this a whirl, TheBigAdmin1


    Pastebin'd because it got large over time.


    <?php custom_badgeos_user_achievements(12,1); ?>

    You get two parameters, and they should be documented in the pastebin as well. Basically pass in how many badges to fetch, and then the author ID you want. It will default to the current author page's ID if none is passed in. It allows you show specific author/members somewhere if you want.

    Let me know if you need a bit more usage help, or if it's not working quite as it should.

  9. Michael Beckwith
    Posted 3 years ago #

    Hokay, TheBigAdmin1,

    In short, ignore my last reply, follow this one.

    I've been given this as a recommended solution for you instead of what I have above. I've looked it over myself and can already see where it does better than what I was doing, so I'll recommend it myself as well. Put the contents of the gist in your functions.php file.


    Put the following function call wherever you want them displayed:


    It takes 3 parameters but none are required. If none provided, it'll fallback to defaults.
    Author ID (default 0), Achievement Type (default all, if left empty), and limit (default 1)

    No caching involved in this version as it's not having to do any laborious querying, so that's always a good thing. I did forget that once you have the ID, you can get most anything easily.

  10. TheBigAdmin1
    Posted 3 years ago #

    Micheal, the code worked flawlessly within the authors.php.. How do i tweek the code you provided to show the badge alt text when hovered as apposed to the badge name? And are the badges listed in collumns? Right now I see two badges listed under each other.

    Excellent code.. We hope to thank you in more ways than one as the site continues to grow.

  11. Michael Beckwith
    Posted 3 years ago #

    It looks like one would have to edit the core BadgeOS functions to get to the spot where the image tag is being generated, for which I'd have to advise against right now. However, for accessibility purposes, alt tags should be added in a future release and also made filterable for those wanting to change them. For that, I'll be adding an issue to the GitHub repo.

    If you look in the gist above, you'll see that each badge should be wrapped in a div that has a class, as well as each badge title be wrapped in a span that has a class. Those were added to make it easy for one to hook onto via css. That said, I didn't do anything in the way of css to alter this, so it'll depend on your theme to style as necessary and to fit.

  12. TheBigAdmin1
    Posted 3 years ago #


    Sorry for the late response but I wanted to thank you again for the help. Do you have any tips on how we could improve our site. Do you know of any developers who might be interested in contributing their knowledge to help enhance the site?

  13. Michael Beckwith
    Posted 3 years ago #

    I'd prefer to stay neutral on the tips part as i'm not much of a designer, and I don't really know of any freelancers who would be able to give some time.

  14. Brad Williams
    Plugin Author

    Posted 3 years ago #

    This appears to be resolved

  15. smallblacksticky
    Posted 2 years ago #

    I have a question for Michael Beckwith.
    The code works good for me, but since I'm not very good with PHP I got a small problem. How can I change the code to display the newest earned badge? Right now I only see the very first badges the user gained.

    Regards Johanna

  16. Michael Beckwith
    Posted 2 years ago #

    Johanna, if you could let me know which bit of code you're using from my previous replies, I can look into it and help you get the latest badges first.

  17. smallblacksticky
    Posted 2 years ago #

    Hi! Yes of course! I used the code from this link you shared before: https://gist.github.com/tw2113/5de0862e7d0b4be59b2c

    Plus the: custom_badgeos_get_author_achievements();

  18. Michael Beckwith
    Posted 2 years ago #

    see if this helps, on line 21, after the $achievements variable is created on line 20, add this

    $achievements = array_reverse( $achievements );

    I wager the badgeos_get_user_earned_achievement_ids() is going from oldest to newest as, in the long run, it fetches values stored on a user meta key, which logically would have the first badges at the start of the saved value.

  19. smallblacksticky
    Posted 2 years ago #

    It, worked! Thank you so much!

  20. Ala7lam
    Posted 2 years ago #

    Very useful codes Michael,
    Thanks for helping the not so pro programmers like us :).

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • BadgeOS
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic