Make WordPress Core


Ignore:
Timestamp:
03/09/2025 09:14:28 AM (12 months ago)
Author:
audrasjb
Message:

Media: Improve HTML5 compliance of wp_video_shortcode() boolean attributes.

This changeset updates wp_video_shortcode() to improve boolean attributes handling in accordance with HTML5 standards. Technically, it replaces attr="1" with attr for the loop, autoplay and muted attributes. The preload attribute is also updated to accept only allowed values: none, metadata, and auto. If a value outside of this list is provided, it will be ignored, preventing invalid attribute outputs.

Props jongycastillo, sabernhardt, joedolson, audrasjb, shub07, debarghyabanerjee.
Fixes #60178.

File:
1 edited

Legend:

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

    r59865 r59954  
    34833483    $attr_strings = array();
    34843484
    3485     foreach ( $html_atts as $k => $v ) {
    3486         $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
     3485    foreach ( $html_atts as $attribute_name => $attribute_value ) {
     3486        if ( in_array( $attribute_name, array( 'loop', 'autoplay', 'muted' ), true ) && true === $attribute_value ) {
     3487            // Add boolean attributes without their value for true.
     3488            $attr_strings[] = esc_attr( $attribute_name );
     3489        } elseif ( 'preload' === $attribute_name && ! empty( $attribute_value ) ) {
     3490            // Handle the preload attribute with specific allowed values.
     3491            $allowed_preload_values = array( 'none', 'metadata', 'auto' );
     3492            if ( in_array( $attribute_value, $allowed_preload_values, true ) ) {
     3493                $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3494            }
     3495        } elseif ( ! empty( $attribute_value ) ) {
     3496            // For non-boolean attributes, add them with their value.
     3497            $attr_strings[] = sprintf( '%s="%s"', esc_attr( $attribute_name ), esc_attr( $attribute_value ) );
     3498        }
    34873499    }
    34883500
Note: See TracChangeset for help on using the changeset viewer.