Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Last active October 14, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcernis/434a870494b3f1dc03ae5dd22f58131d to your computer and use it in GitHub Desktop.
Save nickcernis/434a870494b3f1dc03ae5dd22f58131d to your computer and use it in GitHub Desktop.
Prevent 'NotAllowedError' and 'user didn't interact with the document first' errors for Vimeo embedded in WordPress. No longer needed in WP 5.3 beta 3 or higher.
Add the code in the functions.php file below to your active theme's functions.php file.
You can remove the code once WordPress updates to media element 4.2.10+. (WordPress 5.3 beta 3 updated from 4.2.6 to 4.2.13: https://wordpress.org/news/2019/10/wordpress-5-3-beta-3/)
add_action( 'wp_enqueue_scripts', 'studiopress_load_newer_vimeo_mediaelement', 100 );
/**
* Replace the mediaelement-vimeo script with a newer version.
*
* This works around an issue in WordPress core that can prevent playback of
* Vimeo videos embedded via the `wp_video_shortcode()` function.
*
* This is caused by WordPress loading an older version of the Vimeo script
* (currently loads 4.2.6), which does not add the 'allow=autoplay'
* attribute to iframes that Chrome now requires to permit video playback as
* the first user action on a page.
*
* See https://github.com/mediaelement/mediaelement/issues/2618.
*
* This code and the `add_action` line above could be removed once WordPress
* updates to use Media Element 4.2.10+.
*/
function studiopress_load_newer_vimeo_mediaelement() {
wp_dequeue_script( 'mediaelement-vimeo' );
wp_deregister_script( 'mediaelement-vimeo' );
wp_enqueue_script(
'mediaelement-vimeo',
'https://cdn.jsdelivr.net/gh/mediaelement/mediaelement@4.2.10/src/js/renderers/vimeo.js',
array( 'mediaelement' ),
'4.2.10',
1
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment