Remove autoplay from Video and Cover blocks so videos only play when visitors press play, and add controls where they're missing.
add_filter( 'render_block', function ( string $html, array $block ): string {
if ( ! in_array( $block['blockName'] ?? '', [ 'core/video', 'core/cover' ], true ) || ! str_contains( $html, '<video' ) ) {
return $html;
}
$tags = new WP_HTML_Tag_Processor( $html );
while ( $tags->next_tag( 'video' ) ) {
$tags->remove_attribute( 'autoplay' );
// Background videos in Cover blocks need a way to start them.
if ( null === $tags->get_attribute( 'controls' ) ) {
$tags->set_attribute( 'controls', true );
}
}
return $tags->get_updated_html();
}, 10, 2 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Video that starts on its own is distracting for many people and can interfere with screen readers. WCAG asks that anything moving for more than five seconds can be paused. This snippet makes every video wait for the visitor.
render_block filters each block’s HTML as the page is built. For Video and Cover blocks, WP_HTML_Tag_Processor finds the <video> tag, removes autoplay, and adds controls if there were none, so the video can be started.
View a page with a Video block set to autoplay. It should no longer start on its own, and should show player controls.