I am trying to develop a theme in which when you select a 'video' post format, it automatically gets the first video from the post and features it (instead of where the featured image would normally be) similar to the way the 'Divi' theme by Elegant Themes works. I have managed to accomplish the actual retrieving of the video embed code and copying it into the featured slot, however this requires that I now need to remove the first video from the post content itself, otherwise I get 2 of the same video appear on the page, one in the featured area and one at the top of the post content.

What I would like to find out is:

  1. What is the easiest way to make the first video of a 'video' post automatically featured?

  2. Once the video is featured how can you remove the video from the post content, preventing the same video being posted twice on the page?

This should work with all video embeds from youtube, vimeo, dailymotion, etc and self hosted video.

EDIT: Here is the code I have so far. It's a couple of functions which take the first video embed and return the embed code of the video to feature. The second function manages to delete the first video but only for 'video' shortcodes. I am trying to figure out how to do this for embedded videos.

function get_video_link(){
    $first_video  = '';
    $custom_fields = get_post_custom();

    foreach ( $custom_fields as $key => $custom_field ) {
        if ( strpos( $key, '_oembed_' ) !== 0 ) {

        $first_video = $custom_field[0];
        $first_video = wrap_embed_html($first_video);


    if ( $first_video === '' && has_shortcode( get_the_content(), 'video' )  ) {
        $regex = get_shortcode_regex();
        preg_match( "/{$regex}/s", get_the_content(), $match );

        add_filter('the_content', 'et_delete_post_video_shortcode');

        $first_video = wrap_embed_html(do_shortcode($match[0]));

    return ( $first_video !== '' ) ? $first_video : false;

function et_delete_post_video_shortcode( $content ) {
    if ( has_post_format( 'video' ) ) :
        $regex = get_shortcode_regex();
        preg_match_all( "/{$regex}/s", $content, $matches );

        // $matches[2] holds an array of shortcodes names in the post
        foreach ( $matches[2] as $key => $shortcode_match ) {
            if ( 'video' === $shortcode_match ) {
                $content = str_replace( $matches[0][$key], '', $content );

    return $content;
  • 1
    I have added the code above – Dannyd86 Jan 21 '16 at 17:18
  • 1
    I am thinking along the lines of using the apply_filter function to filter out the first video from the post content, but not sure how to go about this. – Dannyd86 Jan 21 '16 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.