Skip to content

Instantly share code, notes, and snippets.

@contemplate
Created November 17, 2024 21:35
Show Gist options
  • Save contemplate/d4745bdf81c45079887a8ac422d9d677 to your computer and use it in GitHub Desktop.
Save contemplate/d4745bdf81c45079887a8ac422d9d677 to your computer and use it in GitHub Desktop.
LearnDash Lesson Progress Bar shortcode
// 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();
}
@contemplate
Copy link
Author

contemplate commented Nov 17, 2024

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"]

Replace 123 with the desired lesson ID.

Notes:

The shortcode assumes that the user is logged in. If not, it will return an empty string.
The progress bar includes both topics and quizzes under the specified lesson.
The array attribute can be set to true if you want the shortcode to return an array of progress data instead of HTML. For example:

`[learndash_lesson_progress lesson_id="123" array="true"]`

Explanation:

User ID Handling: If user_id is not provided, the shortcode uses the ID of the currently logged-in user.
Lesson ID Handling: If lesson_id is not provided, the shortcode checks if it's on a lesson page and uses that ID. If not, it returns an empty string.
Progress Calculation:
    Retrieves all topics and quizzes associated with the lesson.
    Counts the total number of steps and the number of completed steps.
    Calculates the completion percentage.
Output: Generates HTML for a progress bar similar to the one used in your provided code.

Remember to add this code to your theme's functions.php file or a custom plugin to ensure it works correctly.

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