Created
June 25, 2015 16:18
-
-
Save FriendlyWP/59a5dc4c3e7cc445adf7 to your computer and use it in GitHub Desktop.
Embed Twitter Cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helper function for twitter card stuff | |
// GET YOUTUBE ID FROM URL | |
function getYouTubeIdFromURL($url) { | |
$pattern = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i'; | |
preg_match($pattern, $url, $matches); | |
return isset($matches[1]) ? $matches[1] : false; | |
} | |
function fw_twitter_card_stuff() { | |
global $post; | |
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "medium" ); | |
$title = the_title_attribute( "echo=0" ); | |
$excerpt = strip_tags(get_the_excerpt($post->ID)); | |
// GLOBAL TWITTER CARD META | |
$card_meta .= '<meta name="twitter:site" content="@TWITTERSITEUSER">' . "\n"; | |
$card_meta .= '<meta name="twitter:creator" content="@TWITTERSITEUSER">' . "\n"; | |
$card_meta .= '<meta name="twitter:title" content="' . $title . '">' . "\n"; | |
$card_meta .= '<meta name="twitter:domain" content="DOMAIN NAME"/>' . "\n"; | |
if ($excerpt) { | |
$card_meta .= '<meta name="twitter:description" content="' . $excerpt . '">' . "\n"; | |
} | |
if (has_post_thumbnail($post->ID)) { | |
$card_meta .= '<meta name="twitter:image:src" content="' . $thumb[0] . '">' . "\n"; | |
} | |
// TWITTER VIDEO PLAYER META | |
// assumes here Advanced Custom Fields (get_field) and a custom field holding the YouTube URL | |
if ( function_exists("get_field") && (get_field("new_video_url", $post->ID) || get_field("add_video_url", $post->ID)) ) { | |
if (get_field("new_video_url", $post->ID) !== '') { | |
$videourl = get_field("new_video_url", $post->ID); | |
} elseif (get_field("add_video_url", $post->ID) !== '') { | |
$videourl = get_field("add_video_url", $post->ID); | |
} | |
$embedurl = getYouTubeIdFromURL($videourl); | |
$card_meta .= '<meta name="twitter:card" content="player">' . "\n"; | |
// SHOW THE EMBED VERSION OF THIS LINK | |
$card_meta .= '<meta name="twitter:player" content="https://www.youtube.com/embed/' . $embedurl . '">' . "\n"; | |
$card_meta .= '<meta name="twitter:player:width" content="560">' . "\n"; | |
$card_meta .= '<meta name="twitter:player:height" content="315">' . "\n"; | |
$card_meta .= '<meta name="twitter:player:stream" content="https://www.youtube.com/embed/' . $embedurl . '">' . "\n"; | |
// IF NO VIDEO, LOOK FOR THUMBNAIL | |
} elseif (has_post_thumbnail($post->ID)) { | |
$card_meta .= '<meta name="twitter:card" content="summary_large_image">' . "\n"; | |
// IF NO THUMBNAIL, SHOW SUMMARY ONLY | |
} else { | |
$card_meta .= '<meta name="twitter:card" content="summary" />' . "\n"; | |
} | |
return $card_meta; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment