0

I'm using the following code to find first YouTube/Vimeo embed in the post content:

function compare_by_offset( $a, $b ) {
    return $a['order'] - $b['order'];
}

function first_video_url($post_id = null) {

    if ( $post_id == null OR $post_id == '' ) $post_id = get_the_ID();

    $post_array = get_post( $post_id );

    $markup = $post_array->post_content;

    $regexes = array(
        '#(?:https?:)?//www\.youtube(?:\-nocookie)?\.com/(?:v|e|embed)/([A-Za-z0-9\-_]+)#', // Comprehensive search for both iFrame and old school embeds
        '#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube(?:\-nocookie)?\.com/watch\?.*v=([A-Za-z0-9\-_]+)#', // Any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
        '#(?:https?(?:a|vh?)?://)?youtu\.be/([A-Za-z0-9\-_]+)#', // Any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
        '#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', // YouTube Lyte
        '#data-youtube-id="([A-Za-z0-9\-_]+)"#', // LazyYT.js
        '#<object[^>]+>.+?http://vimeo\.com/moogaloop.swf\?clip_id=([A-Za-z0-9\-_]+)&.+?</object>#s', // Standard Vimeo embed code
        '#(?:https?:)?//player\.vimeo\.com/video/([0-9]+)#', // Vimeo iframe player
        '#\[vimeo id=([A-Za-z0-9\-_]+)]#', // JR_embed shortcode
        '#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Another shortcode
        '#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Yet another shortcode
        '#(?:https?://)?(?:www\.)?vimeo\.com/([0-9]+)#', // Vimeo URL
        '#(?:https?://)?(?:www\.)?vimeo\.com/channels/(?:[A-Za-z0-9]+)/([0-9]+)#' // Channel URL
    );


    $provider_videos = array();

    foreach ( $regexes as $regex ) {
        if ( preg_match_all( $regex, $markup, $matches, PREG_OFFSET_CAPTURE ) ) {
            $provider_videos = array_merge( $provider_videos, $matches[0] );
        }
    }

    if ( empty( $provider_videos ) ) return;

    foreach ( $provider_videos as $video ) {
        $videos[] = array(
            'url'    => $video[0],
            'order'  => $video[1]
        );
    }

    usort( $videos, 'compare_by_offset' );

    $first_video_url =  current(array_column($videos, 'url'));

    if ( empty( $first_video_url ) ) return;

    return $first_video_url;

}

Now when I got the link to the first video in the post I want to remove it from the post content. And that's where I'm stuck. My attempt so far:

function remove_first_image ($content) {

    $url = first_video_url();
    $parsed = parse_url($url);
    $video_id = $parsed['query'];
    $embed_code = wp_oembed_get($url);

    $pattern = 'a pattern for that embed which I fail to make';

    $content = preg_replace($pattern, '', $content);

    return $content;
}
add_filter('the_content', 'remove_first_image');

Thanks!

1 Answer 1

0

I guess one couldn't answer his own stupid question until he asks it. Here comes the answer:

function remove_first_image ($content) {

    if ( is_single() && has_post_format('video') ) {

        $url = first_video_url();
        $embed_code = wp_oembed_get($url);

        $content = str_replace($embed_code, '', $content);
    } 

    return $content;
}

add_filter('the_content', 'remove_first_image');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.