My Latest Badge
-
In the demonstration video and on the DMA website there is a “My Latest Badge” display (widget?). How can I achieve this on my site?
-
Could you link me to either one? I’m not sure where to find it personally. Those may be custom implementations for DMA as well.
You can see it, if you look at the top right corner of the screen in the first frame of the video on the BadgeOS in Action page:
https://badgeos.org/about/sample-sites/
And you can see it again about 55 sec into the film.To see it on the DMA page, you’d have to sign up and log into your profile.
Ah gotcha.
The video should do well enough. Without being able to dive deep into any code for it, I’d wager it’s just plucking out the last achievement that a user has earned, from their user meta and displaying information about it using its achievement ID, which is just a post ID.
Try out the following custom function. It may need some tweaking since it can return individual steps, but this will get you on your way:
function my_badgeos_get_latest_achievement( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } $achievements = badgeos_get_user_achievements( $user_id ); if ( !empty( $achievements ) ) { return end( $achievements ); } return array(); }Usage:
$latest = my_badgeos_get_latest_achievement(); echo get_the_post_thumbnail( $latest->ID, 'large' );All you really need to do is use the function and assign the return value to a variable, and then you’d have a variable that has the following:
$latest->ID $latest->post_type $latest->poinsts $latest->date_earnedThe ID is the most important part because it’d allow you query for pretty much anything related to the achievement. The post_type is the achievement type its’ related to.
The function declaration simply checks for a user ID to use, and if none is provided, it’ll just use the current user’s. Then it’ll grab the user’s earned achievements and return either an empty array if there are none, or the latest one earned which will be at the end of the saved value.
So I’ve tried a bit myself, but unfortunately my skill level is not high enough to achieve this on my own by just this. Could you help me a bit more on the way with a bit more detailed explanation?
What do you have so far? so that I can build off from there instead of trying something from scratch.
I tried adding the code to a function-plugin, as the function.php is updated regularly with the theme.
Did not work though. Just broke everything, and that’s about as far as I got. 😛
Well, if you still have the file available, lets get it shared and we can move from there, if not, I’ll just start a quick plugin that can be shared and has an example or two for output.
I think the easiest thing would be to start a new plugin if you wouldn’t mind?
What type of quick layout do you want for this? Just the large version of the achievement image and perhaps the achievement name below it?
Does it need to be for the current user? or the displayed user when browsing user profiles say in BuddyPress?
Any changes or updates to this one elmindo?
The topic ‘My Latest Badge’ is closed to new replies.