# Stop Audio and Unmuted Videos From Autoplaying

> Removes the autoplay setting from audio players and from videos that are not muted in your content, so sound never starts without the visitor choosing to play it.

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

## Code

```php
add_filter( 'the_content', function ( $content ) {
    if ( false === stripos( $content, 'autoplay' ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return $content;
    }

    $tags = new WP_HTML_Tag_Processor( $content );

    while ( $tags->next_tag() ) {
        $tag = $tags->get_tag();

        if ( ( 'AUDIO' === $tag || ( 'VIDEO' === $tag && null === $tags->get_attribute( 'muted' ) ) ) && null !== $tags->get_attribute( 'autoplay' ) ) {
            $tags->remove_attribute( 'autoplay' );
            $tags->set_attribute( 'controls', true );
        }
    }

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

## Notes

Muted background videos keep autoplaying; use the reduced-motion video snippet if those should stop too.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Stop Audio and Unmuted Videos From Autoplaying" 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/
