Skip to content

Instantly share code, notes, and snippets.

@contemplate
Created July 19, 2024 21:15
Show Gist options
  • Save contemplate/a894087b48fdec24984633228a6de072 to your computer and use it in GitHub Desktop.
Save contemplate/a894087b48fdec24984633228a6de072 to your computer and use it in GitHub Desktop.
LearnDash Category Progress shortcode
add_shortcode( 'learndash_category_progress', 'learndash_category_progress', 10, 3 );
function learndash_category_progress( $atts = array(), $content = '', $shortcode_slug = 'learndash_category_progress' ) {
global $learndash_shortcode_used;
$learndash_shortcode_used = true;
$atts = shortcode_atts(
array(
'course_category_id' => 0,
'user_id' => 0,
'array' => false,
),
$atts
);
if ( empty( $atts['user_id'] ) ) {
if ( is_user_logged_in() ) {
$atts['user_id'] = get_current_user_id();
}
}
if ( empty( $atts['course_category_id'] ) ) {
return '';
}
// Get all courses in the specified category.
$courses = get_posts( array(
'post_type' => 'sfwd-courses',
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'ld_course_category',
'field' => 'term_id',
'terms' => $atts['course_category_id'],
),
),
) );
if ( empty( $courses ) ) {
return '';
}
$total_completed = 0;
$total_steps = 0;
foreach ( $courses as $course ) {
$course_progress = learndash_user_get_course_progress( $atts['user_id'], $course->ID );
if ( isset( $course_progress['completed'] ) ) {
$total_completed += absint( $course_progress['completed'] );
}
if ( isset( $course_progress['total'] ) ) {
$total_steps += absint( $course_progress['total'] );
}
}
$percentage = 0;
if ( $total_steps > 0 ) {
$percentage = intval( $total_completed * 100 / $total_steps );
$percentage = ( $percentage > 100 ) ? 100 : $percentage;
}
$message = sprintf( esc_html_x( '%1$d out of %2$d steps completed', 'placeholders: completed steps, total steps', 'learndash' ), $total_completed, $total_steps );
if ( $atts['array'] ) {
return array(
'percentage' => $percentage,
'completed' => $total_completed,
'total' => $total_steps,
);
}
ob_start();
?>
<div class="learndash-wrapper learndash-widget">
<div class="ld-progress ld-progress-inline">
<div class="ld-progress-heading">
<div class="ld-progress-stats">
<div class="ld-progress-percentage ld-secondary-color">
<?php
echo sprintf(
esc_html_x( '%s%% Complete', 'placeholder: Progress percentage', 'learndash' ),
esc_html( $percentage )
);
?>
</div>
<div class="ld-progress-steps">
<?php
echo sprintf(
esc_html_x( '%1$d/%2$d Steps', 'placeholders: completed steps, total steps', 'learndash' ),
esc_html( $total_completed ),
esc_html( $total_steps )
);
?>
</div>
</div> <!--/.ld-progress-stats-->
</div>
<div class="ld-progress-bar">
<div class="ld-progress-bar-percentage ld-secondary-background" style="<?php echo esc_attr( 'width:' . $percentage . '%' ); ?>"></div>
</div>
</div> <!--/.ld-progress-->
</div>
<?php
return ob_get_clean();
}
@contemplate
Copy link
Author

This shortcode shows a single progress bar for ALL courses in a specific Course Category.
Example usage:
[learndash_category_progress course_category_id="123" user_id="456"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment