Simpler way to display badgeOS thumbnails with custom URL

Good day,
My limited knowledge of php has ended up creating a monstrosity of code. I am hoping someone could show me a simpler, cleaner way to accomplish this. Unfortunately I’m really really bad with php.

What I’m trying to accomplish

Read More

I am trying to code in some badgeOS icons for my WordPress site that link to corresponding lesson posts. In other words, the thumbnail from badge_post_id should link to a specific lesson_post_id. The badge thumbnail also needs to have a different look for ‘earned’ and ‘unearned’.

I have the following code that each badge thumbnail currently uses:

<!-- Badge must either be marked earned or unearned -->
<?php if (in_array($badge_post_id, $earned_achievements)) {
    $css_class = 'earned-badge'; }
else {
    $css_class = 'unearned-badge'; }
?>

<!-- Each badge requires this code -->
<span id="lesson-#" class="badge-class">
    <a href="http://example.com/?p=$lesson_post_id" class="badge-link">
        <span class="badge-icon">
            <?php echo badgeos_get_achievement_post_thumbnail($badge_post_id, $image_size, $css_class) ;?>
        </span>
        <span class="badge-name"> LESSON TITLE </span>
    </a>
</span>

I’m also using this at the start of the page:

<!-- Everything gets this -->
<?php
    $image_size = array(100,100);
    $earned_achievements = badgeos_get_user_earned_achievement_ids(get_current_user_id());
?>

As you can see, for 30+ badges this is a TON of code, and a huge headache to keep track of.

Ideally it would pull all the needed info from some kind of table (perhaps an array of some kind) that relates all this info:

Lesson number: $badge_post_id, $lesson_post_id, $lesson_title, $css_class (earned or unearned)

So I can type someone much simpler to get each badge instead of adding 100 lines of code.

Related posts

2 comments

  1. Something like this?

    <?php
    $earned_achivements = badgeos_get_user_earned_achivement_ids();
    $badges = get_badges();
    
    
    foreach ($badges as $badge){
        if (in_array($badge["id"], $earned_achievements)) {
            echo '<span id="lesson-'.$badge["lesson_id"].'" class="earned-badge">';
            $badge_output = badgeos_get_achievement_post_thumbnail( $badge["id"], $image_size, 'earned-badge' );
        } else {
            echo '<span id="lesson-'.$badge["lesson_id"].'" class="unearned-badge">';
            $badge_output = badgeos_get_achievement_post_thumbnail( $badge["id"], $image_size, 'unearned-badge' );
        }
        echo '<a href="http://example.com/?p='.$badge["lesson_id"].'" class="badge-link">';
        echo '<span class="badge-icon">';
        echo $badge_output;
        echo '</span><span class="badge-name">'.$badge["lesson_title"].'</span>';
        echo '</a></span>';
    }
    

    $badges could be a raw grab of a db table with columns id, lesson_id, lesson_title and whatever else you need.

  2. @hendr1x
    Your help led to me to this solution! Rather than enter the data in an array or table to be only used once, I used your code and placed it as a function.

        function badgeosm($lesson_id, $badge_id){
    
            $image_size = array(100,100);
            $earned_achievements = badgeos_get_user_earned_achievement_ids( get_current_user_id() );
            $css_class = 'unearned-badge'; // Set unearned-badge as the default.
    
            if (in_array($badge_id, $earned_achievements)) {
                    $css_class = 'earned-badge';
                }
            echo '<span id="lesson-ID-'.$lesson_id.'" class="badge-class">';
            echo '<a href="http://example.com/?p='.$lesson_id.'" class="badge-link">';
            echo '<span class="badge-icon">';
            echo badgeos_get_achievement_post_thumbnail( $badge_id, $image_size, $css_class );
            echo '</span>';
            echo '<span class="badge-name">';
            echo get_the_title( $lesson_id );
            echo '</span>';
            echo '</a>';
    
        }
    badgeosm(1961,1770);
    

    Now I just enter the id of the lesson and badge, and the function does the rest. It seems to work well, and it certainly reduces code. Thanks so much. 🙂

Comments are closed.