Skip to content

Instantly share code, notes, and snippets.

@websupporter
websupporter / remove-autoembed.php
Created March 16, 2016 09:47
oEmbed in WordPress
<?php
/**
* Remove the autoemed functionality in WordPres
*
* By using this filter, URLs from Youtube, Twitter etc. won't transform into embeded contents
**/
add_action( 'init', 'fwe_remove_auto_embed' );
function fwe_remove_auto_embed(){
remove_filter(
@websupporter
websupporter / add-gist-as-oembed-provider.php
Last active March 16, 2016 10:03
This script adds Github Gist to the registered oEmbed providers in WordPress
<?php
/**
* This script adds Github Gist to the registered oEmbed providers in WordPress
**/
wp_embed_register_handler( 'gist', '/https:\/\/gist\.github\.com\/(.*)\/(.*)\/?/i', 'wp_embed_handler_gist' );
function wp_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%1$s/%2$s.js"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
@websupporter
websupporter / add-gfycat.php
Created March 16, 2016 10:37
Add Gfycat as oEmbed provider
<?php
/**
* Add Gfycat as oEmbed provider
**/
wp_oembed_add_provider(
'#http(s)?://(www\.)?gfycat\.com/.*#i',
'https://api.gfycat.com/v1/oembed?url=',
true
);
?>
<?php
/**
* If you have already included Gist as a handler, you could
* use this Snippet to include the raw output as iframe...
* ... if... well if github would permit it... but
* just for demonstration purposes.
* **/
add_filter( 'embed_handler_html', 'fwe_embed_handler_html', 10, 4 );
function fwe_embed_handler_html( $html, $url, $attr, $post_ID = null ){
@websupporter
websupporter / oembed-youtube-thumbnails.php
Last active May 8, 2020 05:21
Replace the Youtube embed with a thumbnail
<?php
add_filter( 'oembed_result', 'fwe_oembed_result', 10, 3 );
function fwe_oembed_result( $result, $url, $args ){
//Uebernommen aus wp-includes/class-oembed.php
//L38ff
$youtube_regex = array(
'#http://((m|www)\.)?youtube\.com/watch.*#i',
'#https://((m|www)\.)?youtube\.com/watch.*#i',
'#http://youtu\.be/.*#i',
'#https://youtu\.be/.*#i',
@websupporter
websupporter / script.js
Created March 18, 2016 11:43
JS Handling for Youtube Thumbnail Replace
jQuery( document ).ready( function(){
jQuery( '.youtube-wrapper img' ).click( function(){
var id = jQuery( this ).parent().attr( 'data-youtube' );
var html = '<iframe width="480" height="270" src="https://www.youtube.com/embed/' + id + '?feature=oembed" frameborder="0" allowfullscreen></iframe>';
jQuery( this ).parent().html( html );
});
});
@websupporter
websupporter / oembed-youtube-thumbnails-alternative.php
Created March 18, 2016 11:56
Replace with thumbnail via oembed_dataparse
<?php
add_filter( 'oembed_dataparse', 'fwe_oembed_dataparse', 10, 3 );
function fwe_oembed_dataparse( $result, $data, $url ){
//Uebernommen aus wp-includes/class-oembed.php
//L38ff
$youtube_regex = array(
'#http://((m|www)\.)?youtube\.com/watch.*#i',
'#https://((m|www)\.)?youtube\.com/watch.*#i',
'#http://youtu\.be/.*#i',
'#https://youtu\.be/.*#i',
@websupporter
websupporter / change-oembed-output.php
Last active February 1, 2022 15:36
Change the oEmbed output of WordPress
<?php
add_filter( 'oembed_response_data', 'fwe_oembed_response_data', 11, 4 );
function fwe_oembed_response_data( $data, $post, $width, $height ){
if ( ! is_object( $post ) )
return $data;
if( ! has_post_thumbnail( $post->ID ) )
return $data;
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$image = wp_get_attachment_image_src( $thumbnail_id, 'full' );
<?php
/**
* Contains the post embed template.
*
* When a post is embedded in an iframe, this file is used to
* create the output.
*
* @package WordPress
* @subpackage oEmbed
* @since 4.4.0
<?php
/**
* Contains the post embed content template part.
*
* When a post is embedded in an iframe, this file is used to
* create the content template part output if the active theme does not include
* a content-embed.php template.
*
* @package WordPress
* @subpackage Theme_Compat