Skip to content

Instantly share code, notes, and snippets.

@jas8522
Last active October 20, 2020 13:56
Show Gist options
  • Save jas8522/5f65a3d71c1cf5d90e1e51eec38864b9 to your computer and use it in GitHub Desktop.
Save jas8522/5f65a3d71c1cf5d90e1e51eec38864b9 to your computer and use it in GitHub Desktop.
Show first video found in content on post grid
/**
* Option 1: Replace the featured image spot with the video embed
*/
function ws_replace_image_with_first_embed($html, $post_id, $post_thumbnail_id, $size, $attr){
$post = get_post($post_id);
if (has_category('videos', $post)){
//Get the content, apply filters and execute shortcodes
$content = apply_filters( 'the_content', $post->post_content );
$embeds = get_media_embedded_in_content( $content );
if (!empty($embeds)){
return $embeds[0];
}
}
return $html;
}
add_action( 'post_thumbnail_html', 'ws_replace_image_with_first_embed', 10, 5 );
/**
* Option 2: Output the video regardless of the featured image. This one is for BeaverBuilder's post grid hook
*/
function ws_show_first_embedded_video($settings, $module){
$post = get_post();
if (has_category('videos', $post)){
//Get the content, apply filters and execute shortcodes
$content = apply_filters( 'the_content', $post->post_content );
$embeds = get_media_embedded_in_content( $content );
if (!empty($embeds)){
echo $embeds[0];
}
}
}
add_action( 'fl_builder_post_grid_before_content', 'ws_show_first_embedded_video', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment