Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active November 9, 2023 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/8199d0fcb417a909380b0aeff4247cf7 to your computer and use it in GitHub Desktop.
Save westonruter/8199d0fcb417a909380b0aeff4247cf7 to your computer and use it in GitHub Desktop.
<?php
/**
* Make YouTube Accessible Embed Plugin
*
* @package TBK_YouTube_Embed_Mods
* @author Weston Ruter
* @link https://gist.github.com/westonruter/8199d0fcb417a909380b0aeff4247cf7
* @license GPL-2.0-or-later
* @copyright 2023 Google Inc.
*
* @wordpress-plugin
* Plugin Name: Make YouTube Accessible Embed
* Plugin URI: https://gist.github.com/westonruter/8199d0fcb417a909380b0aeff4247cf7
* Description: Modify YouTube embeds to include additional IFrame Player API parameters to improve accessibility.
* Version: 0.1.0
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Update URI: false
*/
// Note: The embed_oembed_html filter can't be used because of Jetpack.
add_filter(
'the_content',
static function ( $content ) {
if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
return $content;
}
$p = new WP_HTML_Tag_Processor( $content );
while ( $p->next_tag( [ 'tag_name' => 'iframe' ] ) ) {
$src = $p->get_attribute( 'src' );
if (
is_string( $src )
&&
wp_parse_url( $src, PHP_URL_HOST ) === 'www.youtube.com'
&&
str_starts_with( wp_parse_url( $src, PHP_URL_PATH ), '/embed/' )
) {
$src = add_query_arg(
[
'rel' => '0', // See <https://developers.google.com/youtube/player_parameters?csw=1#rel>.
'fs' => '0', // See <https://developers.google.com/youtube/player_parameters?csw=1#fs>.
'modestbranding' => '1', // See <https://developers.google.com/youtube/player_parameters?csw=1#modestbranding>.
],
$src
);
$p->set_attribute( 'src', $src );
}
}
return $p->get_updated_html();
},
100
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment