Created
November 17, 2024 21:35
-
-
Save contemplate/d4745bdf81c45079887a8ac422d9d677 to your computer and use it in GitHub Desktop.
LearnDash Lesson Progress Bar shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Register the shortcode | |
add_shortcode( 'learndash_lesson_progress', 'learndash_lesson_progress_bar' ); | |
function learndash_lesson_progress_bar ( $atts = array(), $content = '' ) { | |
global $learndash_shortcode_used; | |
$learndash_shortcode_used = true; | |
$atts = shortcode_atts( | |
array( | |
'lesson_id' => 0, | |
'user_id' => 0, | |
'array' => false, | |
), | |
$atts | |
); | |
// Get the user ID | |
if ( empty( $atts['user_id'] ) ) { | |
if ( is_user_logged_in() ) { | |
$atts['user_id'] = get_current_user_id(); | |
} else { | |
// User is not logged in | |
return ''; | |
} | |
} | |
// Get the lesson ID | |
if ( empty( $atts['lesson_id'] ) ) { | |
$post_id = get_the_ID(); | |
if ( get_post_type( $post_id ) == 'sfwd-lessons' ) { | |
$atts['lesson_id'] = $post_id; | |
} else { | |
// Not on a lesson page and no lesson_id provided | |
return ''; | |
} | |
} | |
$lesson_id = intval( $atts['lesson_id'] ); | |
$user_id = intval( $atts['user_id'] ); | |
// Get topics under the lesson | |
$topics = learndash_get_topic_list( $lesson_id ); | |
// Get quizzes under the lesson | |
$quizzes = learndash_get_lesson_quiz_list( $lesson_id, $user_id ); | |
// Combine topics and quizzes | |
$steps = array(); | |
if ( ! empty( $topics ) ) { | |
foreach ( $topics as $topic ) { | |
$steps[] = $topic; | |
} | |
} | |
if ( ! empty( $quizzes ) ) { | |
foreach ( $quizzes as $quiz ) { | |
$steps[] = $quiz; | |
} | |
} | |
$total_steps = count( $steps ); | |
$total_completed = 0; | |
foreach ( $steps as $step ) { | |
$step_id = $step->ID; | |
$status = learndash_is_item_complete( $step_id, $user_id ); | |
if ( $status ) { | |
$total_completed++; | |
} | |
} | |
$percentage = 0; | |
if ( $total_steps > 0 ) { | |
$percentage = intval( $total_completed * 100 / $total_steps ); | |
$percentage = ( $percentage > 100 ) ? 100 : $percentage; | |
} | |
if ( $atts['array'] ) { | |
return array( | |
'percentage' => $percentage, | |
'completed' => $total_completed, | |
'total' => $total_steps, | |
); | |
} | |
// Output the progress bar HTML | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Examples:
Default Usage (on a Lesson Page):
[learndash_lesson_progress]
This will display the progress bar for the lesson associated with the current page.
Specify a Lesson ID:
[learndash_lesson_progress lesson_id="123"]
Notes:
Explanation:
Remember to add this code to your theme's functions.php file or a custom plugin to ensure it works correctly.