Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Display a progress bar based on the user's course progress.
- *
- * @return string Progress bar HTML.
- */
- function wpfusion_user_progress_bar_shortcode() {
- // Get the current user ID.
- $user_id = get_current_user_id();
- if ( ! $user_id ) {
- return esc_html__( 'You must be logged in to see your progress.', 'wp-fusion' );
- }
- // Get the user's course progress meta.
- $course_progress = get_user_meta( $user_id, 'course_progress', true );
- // Ensure the value is numeric.
- $course_progress = is_numeric( $course_progress ) ? (float) $course_progress : 0.0;
- // Calculate the width percentage.
- $width_percentage = ( ( $course_progress + 0.1 ) / 74 ) * 100;
- // Limit width between 0% and 100% to avoid weird overflows.
- $width_percentage = max( 0, min( 100, $width_percentage ) );
- // Build the output HTML.
- ob_start();
- ?>
- <div class="bar-progress" style="width: <?php echo esc_attr( $width_percentage ); ?>%; background-color: #059e00;"></div>
- <?php
- return ob_get_clean();
- }
- add_shortcode( 'user_progress_bar', 'wpfusion_user_progress_bar_shortcode' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement