Skip to content

Instantly share code, notes, and snippets.

@alexstandiford
Last active January 14, 2017 18:39
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 alexstandiford/6a8c9939e9f589918866b5fc3146de04 to your computer and use it in GitHub Desktop.
Save alexstandiford/6a8c9939e9f589918866b5fc3146de04 to your computer and use it in GitHub Desktop.
oEmbed helper class for WordPress
<?php
/**
* Embed helper class for all WordPress embeds
* User: Alex
* Date: 1/13/2017
* Time: 1:11 PM
*/
namespace embedHelper;
class embed{
/**
* Checks if the current post is a specified embed
* @return bool
*/
public static function is($type){
global $post;
$values = [
'video' => get_field('video_embed_code', $post->ID),
'any' => get_field('embed_code'),
];
$embed = $values[$type];
if($embed != ""){
return true;
}
else{
return false;
}
}
/**
* Checks if the current post has an embed
* @return bool
*/
public static function exists(){
if(embed::is('any')){
return true;
}
else{
return false;
}
}
/**
* Grab all of the supported oEmbed types as an array
* @return array
*/
public static function domains(){
$providers = _wp_oembed_get_object()->providers;
$result = [];
foreach($providers as $provider){
$url = $provider[0];
$domain = parse_url($url)['host'];
array_push($result,$domain);
}
return array_unique($result);
}
/**
* Gets the domain name from the list of available oembed domain types
* @param $search
*
* @return bool|mixed
*/
public static function getDomain($search){
foreach(embed::domains() as $domain){
if(strpos($domain,$search) !== false){
$result = $domain;
break;
}
}
if(!isset($result)){
$result = false;
}
return $result;
}
/**
* Checks if an oEmbed is a specific embed
* @param $embed_domain
* @param $domain_to_check
*
class embed{
/**
* Checks if the current post is a specified embed
* @return bool
*/
public static function is($type){
global $post;
$embed = "";
$values = [
'video' => get_field('video_embed_code', $post->ID),
'any' => get_field('embed_code', $post->ID),
];
if($type == 'any'){
foreach($values as $value){
$embed = $value;
if($value != ""){
break;
}
}
}
else{
if(array_key_exists($type,$values)){
$embed = $values[$type];
}
}
if($embed != ""){
return true;
}
else{
return false;
}
}
/**
* Checks if the current post has an embed
* @return bool
*/
public static function exists(){
if(embed::is('any')){
return true;
}
else{
return false;
}
}
/**
* Grab all of the supported oEmbed types as an array
* @return array
*/
public static function domains(){
$providers = _wp_oembed_get_object()->providers;
$result = [];
foreach($providers as $provider){
$url = $provider[0];
$domain = parse_url($url)['host'];
array_push($result,$domain);
}
return array_unique($result);
}
/**
* Gets the domain name from the list of available oembed domain types
* @param $search
*
* @return bool|mixed
*/
public static function getDomain($search){
foreach(embed::domains() as $domain){
if(strpos($domain,$search) !== false){
$result = $domain;
break;
}
}
if(!isset($result)){
$result = false;
}
return $result;
}
/**
* Checks if an oEmbed is a specific embed
* @param $embed_domain
* @param $domain_to_check
*
* @return bool
*/
public static function isFromDomain($embed_domain,$domain_to_check){
$domain_to_check = embed::getDomain($domain_to_check);
if(strpos($embed_domain,$domain_to_check) !== false){
return true;
}
else{
return false;
}
}
/**
* Gets the video embed code if it exists.
* @return bool|mixed|null|string
*/
public static function get(){
switch(true){
case embed::is('video'):
if(get_field('video_embed_code')){
$result = get_field('video_embed_code');
}
else{
$result = get_field('embed_code');
}
break;
case embed::is('any'):
$result = get_field('embed_code');
break;
case get_field('heading_image'):
$result = wp_get_attachment_image(get_field('heading_image'), 'large');
break;
default:
$result = false;
echo the_post_thumbnail('large');
break;
}
return $result;
}
}
<?php
use embedHelper\embed;
/**
* Modifies the soundcloud oEmbed
* @param $url
*
* @return mixed
*/
function modify_soundcloud_embed($url){
if(embed::isFromDomain($url, 'soundcloud')){ //Change "soundcloud" to whatever oEmbed you want to modify
$args = [
'show_comments' => 'false',
'show_user' => 'false',
'show_artwork' => 'false',
'download' => 'false',
'sharing' => 'false',
'buying' => 'false',
'show_playcount' => 'false',
'show_bpm' => 'false',
];
if(is_page_template('landing-page.php')){
$args['auto_play'] = 'true';
}
$params = '?'.http_build_query($args).'&';
$result = str_replace('?', $params, $url);
}
else{
$result = $url;
}
return $result;
}
add_filter('oembed_result', __NAMESPACE__.'\\modify_soundcloud_embed', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment