Make WordPress Core


Ignore:
Timestamp:
07/12/2026 09:04:58 PM (2 months ago)
Author:
joedolson
Message:

Media: Ensure figcaption/figure IDs are unique.

Using the same image multiple times with a figure and fig caption would result in duplicate IDs, creating unclear relationships and breaking 3rd party expectations for anchor targets. Check id attributes and append a counter to ensure each attribute is unique, while retaining the current expectation of a counter-less ID attribute for the first instance. Update tests to verify.

Developed in https://github.com/WordPress/wordpress-develop/pull/11940

Props steelwagstaff, westonruter, jamesbregenzer, joedolson, mukesh27.
Fixes #65315.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r62649 r62698  
    26572657        }
    26582658
     2659        /**
     2660         * @var array{
     2661         *     id: string,
     2662         *     caption_id: string,
     2663         *     align: string,
     2664         *     width: string,
     2665         *     caption: string,
     2666         *     class: string,
     2667         * } $atts
     2668         */
    26592669        $atts = shortcode_atts(
    26602670                array(
     
    26762686        }
    26772687
    2678         $id          = '';
    2679         $caption_id  = '';
    2680         $describedby = '';
     2688        $id               = '';
     2689        $caption_id       = '';
     2690        $describedby      = '';
     2691        $unique_id_value  = '';
     2692        $caption_id_value = '';
    26812693
    26822694        if ( $atts['id'] ) {
    2683                 $atts['id'] = sanitize_html_class( $atts['id'] );
    2684                 $id         = 'id="' . esc_attr( $atts['id'] ) . '" ';
     2695                $atts['id']      = sanitize_html_class( $atts['id'] );
     2696                $unique_id_value = (string) preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['id'] . '-' ) );
     2697                $id              = 'id="' . esc_attr( $unique_id_value ) . '" ';
    26852698        }
    26862699
    26872700        if ( $atts['caption_id'] ) {
     2701                // User explicitly provided a caption_id - make it unique.
    26882702                $atts['caption_id'] = sanitize_html_class( $atts['caption_id'] );
    2689         } elseif ( $atts['id'] ) {
    2690                 $atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] );
    2691         }
    2692 
    2693         if ( $atts['caption_id'] ) {
    2694                 $caption_id  = 'id="' . esc_attr( $atts['caption_id'] ) . '" ';
    2695                 $describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" ';
     2703                $caption_id_value   = preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['caption_id'] . '-' ) );
     2704        } elseif ( $unique_id_value ) {
     2705                // Derive from the already-unique figure ID - guaranteed unique, no need for second call.
     2706                $caption_id_value = 'caption-' . str_replace( '_', '-', $unique_id_value );
     2707        }
     2708
     2709        if ( $caption_id_value ) {
     2710                $caption_id  = 'id="' . esc_attr( $caption_id_value ) . '" ';
     2711                $describedby = 'aria-describedby="' . esc_attr( $caption_id_value ) . '" ';
    26962712        }
    26972713
Note: See TracChangeset for help on using the changeset viewer.