Skip to content

Instantly share code, notes, and snippets.

@goblindegook
Created February 17, 2014 19:22
Show Gist options
  • Save goblindegook/9057159 to your computer and use it in GitHub Desktop.
Save goblindegook/9057159 to your computer and use it in GitHub Desktop.
WordPress plugin to fix oEmbed over HTTPS for services not embedding the correct schema (e.g. YouTube).
<?php
/*
Plugin Name: Secure oEmbed
Plugin URI: https://gist.github.com/goblindegook/9057159
Description: Ensure oEmbed over HTTPS.
Version: 1.0
Author: Luís Rodrigues
License: GPL 3.0
*/
/**
* Replace http:// with https:// in the embed code (before caching).
*
* @param string $data The returned oEmbed HTML.
* @param string $url URL of the content to be embedded.
* @param array $args Optional arguments, usually passed from a shortcode.
*/
function secure_oembed_result ($data, $url, $args)
{
return preg_replace( '/http:\/\//', 'https://', $data );
}
/**
* Replace http:// with https:// in the embed code (after caching).
*
* @param mixed $cache The cached HTML result, stored in post meta.
* @param string $url The attempted embed URL.
* @param array $attr An array of shortcode attributes.
* @param int $post_ID Post ID.
*/
function secure_embed_oembed_html ($cache, $url, $attr, $post_ID)
{
return preg_replace( '/http:\/\//', 'https://', $cache );
}
if (is_ssl()) {
add_filter( 'oembed_result' , 'secure_oembed_result' , 10, 3 );
add_filter( 'embed_oembed_html' , 'secure_embed_oembed_html', 10, 4 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment