Halloween Sale 15% off Ends in See the plans
PHP

Progress Bar Shortcode

Adds a [wpide_progress value="75" label="Funding goal"] shortcode that draws a labeled progress bar, for fundraising goals, skills or project status.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'wp_enqueue_scripts', function () {
    wp_register_style( 'wpide-progress', false, array(), '1.0' );
    wp_add_inline_style( 'wpide-progress', '.wpide-progress{margin:1em 0;}.wpide-progress__head{display:flex;justify-content:space-between;gap:1em;font-size:.9em;margin-bottom:.35em;}.wpide-progress__track{height:12px;border-radius:999px;background:rgba(0,0,0,.08);overflow:hidden;}.wpide-progress__bar{height:100%;border-radius:inherit;background:var(--wpide-progress-color,#2271b1);width:var(--wpide-progress,0%);}@media (prefers-reduced-motion:no-preference){.wpide-progress__bar{animation:wpide-progress-fill 1.2s ease-out both;}}@keyframes wpide-progress-fill{from{width:0;}}' );
} );

add_shortcode( 'wpide_progress', function ( $atts ) {
    $atts  = shortcode_atts( array( 'value' => 0, 'max' => 100, 'label' => '', 'color' => '' ), $atts, 'wpide_progress' );
    $max   = max( 1, (float) $atts['max'] );
    $value = min( $max, max( 0, (float) $atts['value'] ) );
    $pct   = round( $value / $max * 100, 1 );
    $color = sanitize_hex_color( $atts['color'] );

    wp_enqueue_style( 'wpide-progress' );

    return sprintf(
        '<div class="wpide-progress" style="--wpide-progress:%1$s%%;%2$s"><div class="wpide-progress__head"><span>%3$s</span><span>%4$s%%</span></div><div class="wpide-progress__track" role="progressbar" aria-valuemin="0" aria-valuemax="%5$s" aria-valuenow="%6$s"%7$s><div class="wpide-progress__bar"></div></div></div>',
        esc_attr( $pct ),
        $color ? '--wpide-progress-color:' . esc_attr( $color ) . ';' : '',
        esc_html( $atts['label'] ),
        esc_html( number_format_i18n( $pct, $pct == (int) $pct ? 0 : 1 ) ), // phpcs:ignore Universal.Operators.StrictComparisons
        esc_attr( $max ),
        esc_attr( $value ),
        $atts['label'] ? ' aria-label="' . esc_attr( $atts['label'] ) . '"' : ''
    );
} );

Notes

Add color="#16a34a" to change the bar color. The bar fills with a short animation unless the visitor prefers reduced motion.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.