8

I have a function in my wordpress theme that deletes the first embedded video in a video post. See code below. This is in the functions.php file.

/* - function that hides first video in post content - */

function process_embed( $embed ){

    if ( is_single() && get_post_format() === 'video' ) {
        static $post_video_num = 0;

        $post_video_num++;

        // Hide first video in the post content on single video post page
        if ( 1 === $post_video_num ) {
            return '';
        }
    }

    return $embed;
}


add_filter( 'embed_oembed_html', 'process_embed', 10, 3 );
add_filter( 'video_embed_html', 'process_embed' ); // Jetpack

As you can see, if the post is a single post and it's a video format, it will declare a static variable and iterate it everytime a video is in the post. If the static variable $post_video_num is 1 (meaning the first video in the post) it is replaced with blank, removing the first embed video.

This code works fine in my development environment on my local machine. However, it doesn't seem to work on my live server. That is the problem.

While I was debugging, after echoing out the $post_video_num variable, I found out that it won't remove the first video because the variable $post_video_num is 8 (instead of 1, as it should be).

After echoing out the $post_video_num numbers, what is happening is that it echoes out the numbers 1-8 on top of the page, then echoes out 8-12 in place of where the first video should be. Specifically on the live server, the function seems to loop multiple times, which is why it doesn't work.

The core problem, which is what is puzzling me, is that this function works as intended on my local machine but not on the live server, as it does this strange looping thing which I can't explain.

What would cause this function to work on my local machine and not the live server? Is there something I am missing here?

Thanks! I hope this makes sense. You're help is greatly appreciated.

3
  • You have two filters which trigger the function, that's why, I guess, it's running multiple times. Maybe try disabling one of the two filters to determine which one is the one you need.
    – Dexter0015
    Commented Sep 15, 2016 at 13:51
  • I have just tried that. It hasn't made any difference.
    – danny471
    Commented Sep 15, 2016 at 14:03
  • 1
    Some guy had a problem with jetpack and "embed_oembed_html" filter, check out the latest note here - developer.wordpress.org/reference/hooks/embed_oembed_html maybe it will help )))
    – anton
    Commented Feb 2, 2019 at 13:28

1 Answer 1

0

Try something like this:

function realistic_get_first_embed_video($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
         $counter = 0;
         foreach ($embeds as $embed) {
            // Check condition if count is 0 then 
            // it is the first iteration
            if( $counter == 0 ) {
                return '';
            }

             /*
             if (strpos($embed, 'video') || strpos($embed, 'youtube') || strpos($embed, 'vimeo') || strpos($embed, 'dailymotion') || strpos($embed, 'vine') || strpos($embed, 'wordPress.tv') || strpos($embed, 'hulu')) {
                 return $embed;
             }
             */
            $counter = $counter + 1;
         }
     } else {
         //No video embedded found
         return;
     }
 }
2
  • Can you explain what that does differently? What's the commented-out block in the middle for? This doesn't look like it actually returns anything as-is. What's the counter for?
    – Rup
    Commented Oct 16, 2021 at 10:50
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 16, 2021 at 15:18

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.