Skip to content

Instantly share code, notes, and snippets.

@johndigital
Last active June 16, 2016 22:22
Show Gist options
  • Save johndigital/2c050f7e734330256b04 to your computer and use it in GitHub Desktop.
Save johndigital/2c050f7e734330256b04 to your computer and use it in GitHub Desktop.
Override Wordpress oembed defaults for Vimeo
<?php
/*
* Override default vimeo oembed behavior
*/
function set_vimeo_defaults(){
// set vimeo oembed args
// see full list here: developer.vimeo.com/apis/oembed
$args = array(
'color' => 'ffffff',
'title' => false,
'portrait' => false,
'byline' => false,
'api' => true,
'player_id' => uniqid()
);
// set regex and oembed url
$provider_regex = '#https?://(.+\.)?vimeo\.com/.*#i';
$oembed_source = 'http://vimeo.com/api/oembed.{format}?' . http_build_query($args);
// override the default vimeo configuration
return wp_oembed_add_provider( $provider_regex, $oembed_source, true );
}
add_action('init', 'set_vimeo_defaults');
?>
@johndigital
Copy link
Author

Add this to your functions.php file and change the $args as needed. Every time you use wp_oembed_get() with a vimeo link these arguments will be respected.

@drewbaker
Copy link

I wish that the player_id was actually set on the HTML that is returned from the Vimeo oEmbed. Currently the player_id and api parameters are useless.

@drewbaker
Copy link

John, what restricts this to Vimeo? Seems like this would conflict with the default Vimeo embed. I wonder.

@drewbaker
Copy link

This is where I ended up, works great:

/*
 * Override default vimeo oembed behavior
 */
    function set_vimeo_defaults(){

        // Unregister default Vimeo embed
        $format = '#https?://(.+\.)?vimeo\.com/.*#i';
        wp_oembed_remove_provider($format);

        // set vimeo oembed args
        // see full list here: developer.vimeo.com/apis/oembed
        $args = array(
            'color'     => 'ffffff',
            'title'     => false,
            'portrait'  => false,
            'byline'    => false,
            'api'       => true,
            'player_id' => uniqid('vimeo-')
        );

        // set regex and oembed url
        $provider = 'http://vimeo.com/api/oembed.{format}?' . http_build_query($args);

        // override the default vimeo configuration
        return wp_oembed_add_provider($format, $provider, true);
    }
    add_action('init', 'set_vimeo_defaults');

And the JS:

var site = {
    initVideoDetail: function() {
        // Play video
        jQuery(document).on('click', '.slide-video .play-button', function(){
            var $media = jQuery(this).closest('.media');
            var embed = $media.data('embed');

            // Add playing class
            jQuery('body').addClass('video-playing');

            // Add video to page
            $media.append(embed);

            // Size videos here if needed

            // Play video!
            Froogaloop( $media.find('iframe').get(0) ).addEvent( 'ready', site.videoReady);
        });
    },

    videoReady: function(playerId){
        Froogaloop(playerId).api('play');
        Froogaloop(playerId).addEvent( 'finish', site.videoFinished);
    },

    videoFinished: function(){
        //site.videoBreakdown();
    }
}
jQuery(document).ready(function($){
    site.initVideoDetail();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment