WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. TheBigAdmin1
    Member
    Posted 1 year 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.

    http://wordpress.org/extend/plugins/badgeos/

  2. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year 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 );
    endforeach;
    $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;
    wp_reset_postdata();
    endif;
  3. TheBigAdmin1
    Member
    Posted 1 year ago #

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

  4. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year ago #

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

  7. TheBigAdmin1
    Member
    Posted 1 year ago #

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

  8. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    Give this a whirl, TheBigAdmin1

    http://pastebin.com/KCdkGgRS

    Pastebin'd because it got large over time.

    Usage:

    <?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
    Member
    Plugin Author

    Posted 1 year 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.

    https://gist.github.com/tw2113/5de0862e7d0b4be59b2c

    Put the following function call wherever you want them displayed:

    custom_badgeos_get_author_achievements();

    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
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year ago #

    Michael,

    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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year ago #

    This appears to be resolved

  15. smallblacksticky
    Member
    Posted 7 months 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
    Member
    Plugin Author

    Posted 7 months 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
    Member
    Posted 7 months 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
    Member
    Plugin Author

    Posted 7 months 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
    Member
    Posted 7 months ago #

    It, worked! Thank you so much!

  20. Ala7lam
    Member
    Posted 6 months 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

About this Topic