• Resolved jasonkadlec

    (@jasonkadlec)


    Is there an easy way to have the [badgeos_achievement id=””] shortcode display the Congratulations text if the current logged in user is looking at a page/post that is displaying an achievement using the single achievement shortcode?

    I see that there is already a class added… so the shortcode does already do the logic to see if the current user has achieved it… why not have it go all the way and also display the congrats text?

    https://www.ads-software.com/plugins/badgeos/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael Beckwith

    (@tw2113)

    The BenchPresser

    Likely simply never game to mind at the time.

    There is this filter that you could do what you will with.

    apply_filters( 'badgeos_render_achievement', $output, $achievement->ID );

    badgeos_render_achievement() the function is called as part of the shortcode output, so you’d need to utilize the filter to handle it.

    The $output variable is a string holding all the markup that will be displayed. $achievement->ID will hold the ID of the achievement passed in.

    Thread Starter jasonkadlec

    (@jasonkadlec)

    oops – I see you answered my follow up question already.

    Anyway for anyone wanted to show the Congrats text instead of the normal text when using the single achievment shortcode, add this to your functions.php

    add_filter( 'badgeos_render_achievement', 'learnCMS_render_badge', 10, 2 );
    
    function learnCMS_render_badge ($output, $achievement_ID) {
    
            wp_enqueue_script( 'badgeos-achievements' );
    	wp_enqueue_style( 'badgeos-widget' );
    
    	// check if user has earned this Achievement, and add an 'earned' class
    	$earned_status = badgeos_get_user_achievements( array( 'user_id' => $user_ID, 'achievement_id' => absint( $achievement_ID ) ) ) ? 'user-has-earned' : 'user-has-not-earned';
    
    	// Setup our credly classes
    	$credly_class = '';
    	$credly_ID = '';
    
    	// If the achievement is earned and givable, override our credly classes
    	if ( 'user-has-earned' == $earned_status && $giveable = credly_is_achievement_giveable( $achievement_ID, $user_ID ) ) {
    		$credly_class = ' share-credly addCredly';
    		$credly_ID = 'data-credlyid="'. absint( $achievement_ID ) .'"';
    	}
    
    	// Each Achievement
    	$output = '';
    	$output .= '<div id="badgeos-achievements-list-item-' . $achievement_ID . '" class="badgeos-achievements-list-item '. $earned_status . $credly_class .'"'. $credly_ID .'>';
    
    		// Achievement Image
    		$output .= '<div class="badgeos-item-image">';
    		$output .= '<a href="' . get_permalink( $achievement_ID ) . '">' . badgeos_get_achievement_post_thumbnail( $achievement_ID ) . '</a>';
    		$output .= '</div><!-- .badgeos-item-image -->';
    
    		// Achievement Content
    		$output .= '<div class="badgeos-item-description">';
    
    			// Achievement Title
    			$output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink( $achievement_ID ) . '">' . get_the_title( $achievement_ID ) .'</a></h2>';
    
    			// Achievement Short Description
    			$output .= '<div class="badgeos-item-excerpt">';
    			$output .= badgeos_achievement_points_markup( $achievement_ID );
    			if ('user-has-earned' == $earned_status) {
    				$excerpt = badgeos_render_earned_achievement_text($achievement_ID, get_current_user_id());
    	 		} else {
    				$excerpt = !empty( $achievement->post_excerpt ) ? $achievement->post_excerpt : $achievement->post_content;
    			}
    			$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
    			$output .= '</div><!-- .badgeos-item-excerpt -->';
    
    			// Render our Steps
    			if ( $steps = badgeos_get_required_achievements_for_achievement( $achievement_ID ) ) {
    				$output.='<div class="badgeos-item-attached">';
    					$output.='<div id="show-more-'.$achievement_ID.'" class="badgeos-open-close-switch"><a class="show-hide-open" data-badgeid="'. $achievement_ID .'" data-action="open" href="#">' . __( 'Show Details', 'badgeos' ) . '</a></div>';
    					$output.='<div id="badgeos_toggle_more_window_'.$achievement_ID.'" class="badgeos-extras-window">'. badgeos_get_required_achievements_for_achievement_list_markup( $steps, $achievement_ID ) .'</div><!-- .badgeos-extras-window -->';
    				$output.= '</div><!-- .badgeos-item-attached -->';
    			}
    
    		$output .= '</div><!-- .badgeos-item-description -->';
    
    		$output .= '</div><!-- .badgeos-achievements-list-item -->';
    
           return $output;
    }
    Thread Starter jasonkadlec

    (@jasonkadlec)

    thanks Michael!

    Thread Starter jasonkadlec

    (@jasonkadlec)

    OOPS! Note the above code will break!! I forgot that if the achievement is not yet earned, the code that normally renders the achievement breaks as several things rely on $achievement being a post object… so you need to be sure to add:

    $achievement = get_post($achievement_ID);

    Here is the full code again

    add_filter( 'badgeos_render_achievement', 'learnCMS_render_badge', 10, 2 );
    
    function learnCMS_render_badge ($output, $achievement_ID) {
        wp_enqueue_script( 'badgeos-achievements' );
    	wp_enqueue_style( 'badgeos-widget' );
    
    	// check if user has earned this Achievement, and add an 'earned' class
    	$earned_status = badgeos_get_user_achievements( array( 'user_id' => $user_ID, 'achievement_id' => absint( $achievement_ID ) ) ) ? 'user-has-earned' : 'user-has-not-earned';
    
    	// Setup our credly classes
    	$credly_class = '';
    	$credly_ID = '';
    
    	// If the achievement is earned and givable, override our credly classes
    	if ( 'user-has-earned' == $earned_status && $giveable = credly_is_achievement_giveable( $achievement_ID, $user_ID ) ) {
    		$credly_class = ' share-credly addCredly';
    		$credly_ID = 'data-credlyid="'. absint( $achievement_ID ) .'"';
    	}
    	$achievement = get_post($achievement_ID);
    	// Each Achievement
    	$output = '';
    	$output .= '<div id="badgeos-achievements-list-item-' . $achievement_ID . '" class="badgeos-achievements-list-item '. $earned_status . $credly_class .'"'. $credly_ID .'>';
    
    		// Achievement Image
    		$output .= '<div class="badgeos-item-image">';
    		$output .= '<a href="' . get_permalink( $achievement_ID ) . '">' . badgeos_get_achievement_post_thumbnail( $achievement_ID ) . '</a>';
    		$output .= '</div><!-- .badgeos-item-image -->';
    
    		// Achievement Content
    		$output .= '<div class="badgeos-item-description">';
    
    			// Achievement Title
    			$output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink( $achievement_ID ) . '">' . get_the_title( $achievement_ID ) .'</a></h2>';
    
    			// Achievement Short Description
    			$output .= '<div class="badgeos-item-excerpt">';
    			$output .= badgeos_achievement_points_markup( $achievement_ID );
    			if ('user-has-earned' == $earned_status) {
    				$excerpt = badgeos_render_earned_achievement_text($achievement_ID, get_current_user_id());
    	 		} else {
    				$excerpt = !empty( $achievement->post_excerpt ) ? $achievement->post_excerpt : $achievement->post_content;
    			}
    			$output .= wpautop( apply_filters( 'get_the_excerpt', $excerpt ) );
    			$output .= '</div><!-- .badgeos-item-excerpt -->';
    
    			// Render our Steps
    			if ( $steps = badgeos_get_required_achievements_for_achievement( $achievement_ID ) ) {
    				$output.='<div class="badgeos-item-attached">';
    					$output.='<div id="show-more-'.$achievement_ID.'" class="badgeos-open-close-switch"><a class="show-hide-open" data-badgeid="'. $achievement_ID .'" data-action="open" href="#">' . __( 'Show Details', 'badgeos' ) . '</a></div>';
    					$output.='<div id="badgeos_toggle_more_window_'.$achievement_ID.'" class="badgeos-extras-window">'. badgeos_get_required_achievements_for_achievement_list_markup( $steps, $achievement_ID ) .'</div><!-- .badgeos-extras-window -->';
    				$output.= '</div><!-- .badgeos-item-attached -->';
    			}
    
    		$output .= '</div><!-- .badgeos-item-description -->';
    
    		$output .= '</div><!-- .badgeos-achievements-list-item -->';
    
           return $output;
    }
    Michael Beckwith

    (@tw2113)

    The BenchPresser

    So you’re all good to go? or do you need a bit more help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Congratulations Text when using [badgeos_achievement id="" ] shortcode?’ is closed to new replies.