8 deleted 16 characters in body
source | link

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to yours:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to yours:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = apply_filters( 'the_content', $post->post_content );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to yours:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

7 deleted 9 characters in body
source | link

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to your functionsyours:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to your functions:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to yours:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

6 added 40 characters in body
source | link

Due a bug in get_tag_regex(), this won't work properly. See bug #26674.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to your functions:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

Due a bug in get_tag_regex(), this won't work properly. See bug #26674.

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to your functions:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

You can use the get_media_embedded_in_content() function for both shortcode and embedded media (it looks for these HTML tags: audio, video, object, embed, or iframe), just be sure to use content with applied filters and shortcode executed.

For example:

$post_id = 125;
$post = get_post($post_id);

//Get the content, apply filters and execute shortcodes
$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
$embeds = get_media_embedded_in_content( $content );

//$embeds is an array and each item is the HTML of an embedded media.
//The first item of the array is the first embedded media in the content
$fist_embedded = $embeds[0];

Addapting te above code to a function similar to your functions:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //return first embed
        return $embeds[0];

    } else {
        //No embeds found
        return false;
    }

}

To limit videos only you could do something like this:

function get_first_embed_media($post_id) {

    $post = get_post($post_id);
    $content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
    $embeds = get_media_embedded_in_content( $content );

    if( !empty($embeds) ) {
        //check what is the first embed containg video tag, youtube or vimeo
        foreach( $embeds as $embed ) {
            if( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) ) {
                return $embed;
            }
        }

    } else {
        //No video embedded found
        return false;
    }

}

Note: There was a bug in get_media_embedded_in_content() that made this answer not working as expected bellow WP 4.2.

5 added 143 characters in body
source | link
4 added 57 characters in body
source | link
3 added 745 characters in body
source | link
2 added 96 characters in body
source | link
1
source | link