Skip to content

Instantly share code, notes, and snippets.

@daveaspinall
Last active May 10, 2018 08:32
Show Gist options
  • Save daveaspinall/41942c0757a18c7ebf645c404fc01e06 to your computer and use it in GitHub Desktop.
Save daveaspinall/41942c0757a18c7ebf645c404fc01e06 to your computer and use it in GitHub Desktop.
WordPress Image Embed
public function embedWrapper($html, $url, $attr)
{
$classes = ['post-embed'];
if (strpos($url, 'youtu.be') !== false || strpos($url, 'youtube.com') !== false || strpos($url, 'vimeo') !== false) {
$classes[] = 'post-embed--video';
} else if (strpos($url, 'twitter.com') !== false) {
$classes[] = 'post-embed--twitter';
}
$classes = implode(' ', $classes);
return '<figure class="' . $classes . '">' . $html . '</figure>';
}
add_filter('embed_oembed_html', 'embedWrapper', 10, 3);
.post-embed {
margin: 24px 0;
}
.post-embed--video {
position: relative;
padding-top: calc(100% * 9/16);
}
.post-embed--video iframe,
.post-embed--video object {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.post-embed--twitter {
margin-left: auto;
margin-right: auto;
max-width: 500px;
background-color: #fff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment