Advertisement
verygoodplugins

Untitled

Apr 25th, 2025
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Display a progress bar based on the user's course progress.
  4.  *
  5.  * @return string Progress bar HTML.
  6.  */
  7. function wpfusion_user_progress_bar_shortcode() {
  8.  
  9.     // Get the current user ID.
  10.     $user_id = get_current_user_id();
  11.  
  12.     if ( ! $user_id ) {
  13.         return esc_html__( 'You must be logged in to see your progress.', 'wp-fusion' );
  14.     }
  15.  
  16.     // Get the user's course progress meta.
  17.     $course_progress = get_user_meta( $user_id, 'course_progress', true );
  18.  
  19.     // Ensure the value is numeric.
  20.     $course_progress = is_numeric( $course_progress ) ? (float) $course_progress : 0.0;
  21.  
  22.     // Calculate the width percentage.
  23.     $width_percentage = ( ( $course_progress + 0.1 ) / 74 ) * 100;
  24.  
  25.     // Limit width between 0% and 100% to avoid weird overflows.
  26.     $width_percentage = max( 0, min( 100, $width_percentage ) );
  27.  
  28.     // Build the output HTML.
  29.     ob_start();
  30.     ?>
  31.     <div class="bar-progress" style="width: <?php echo esc_attr( $width_percentage ); ?>%; background-color: #059e00;"></div>
  32.     <?php
  33.  
  34.     return ob_get_clean();
  35. }
  36. add_shortcode( 'user_progress_bar', 'wpfusion_user_progress_bar_shortcode' );
  37. ?>
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement