# Turn On Captions by Default in YouTube Embeds

> Adds YouTube's captions setting to embedded videos, so subtitles are shown automatically whenever the video has them.

- Type: PHP
- Runs: Frontend only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: youtube, captions, embeds
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-youtube-captions-on/

## Code

```php
// Preferred caption language ('' lets YouTube decide).
$wpide_caption_lang = '';

add_filter( 'embed_oembed_html', function ( $html, $url ) use ( $wpide_caption_lang ) {
    if ( ! preg_match( '#youtu\.?be#i', $url ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return $html;
    }

    $tags = new WP_HTML_Tag_Processor( $html );

    if ( $tags->next_tag( 'iframe' ) ) {
        $args = array( 'cc_load_policy' => 1 );
        if ( $wpide_caption_lang ) {
            $args['cc_lang_pref'] = $wpide_caption_lang;
        }
        $tags->set_attribute( 'src', add_query_arg( $args, (string) $tags->get_attribute( 'src' ) ) );
    }

    return $tags->get_updated_html();
}, 10, 2 );
```

## Notes

Change $wpide_caption_lang to a language code such as en or fr to prefer that caption language.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Turn On Captions by Default in YouTube Embeds" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
