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. Commented Sep 15, 2016 at 13:51
  • I have just tried that. It hasn't made any difference. 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 ))) Commented Feb 2, 2019 at 13:28

2 Answers 2

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? 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. Commented Oct 16, 2021 at 15:18
0

Here’s a better approach so you reliably hide the first embedded video only ONCE per post, regardless of how many times the filters trigger.

// in functions.php

function hide_first_embed_video_once( $embed, $url = '', $attr = '' ) { static $has_hidden = false;

if ( is_single() && get_post_format() === 'video' ) {

    if ( ! $has_hidden ) {
        // First video → hide it
        $has_hidden = true;
        return '';
    }
}

return $embed;

}

// Attach to both filters if needed add_filter( 'embed_oembed_html', 'hide_first_embed_video_once', 10, 3 ); add_filter( 'video_embed_html', 'hide_first_embed_video_once' );

Improvements & Alternative

If you want to reset between each post, you’ll need to reset the static between posts. Because static will persist for the entire request, but if WordPress is looping posts (e.g., in an archive, or multiple posts on one page), you may need a more granular reset. For example:

// Initialize/reset before content output of each post add_action( 'the_post', function() { // Reset the static indirectly by calling a wrapper or using a global flag // Or set a value in some global or in post meta etc. remove_filter( 'embed_oembed_html', 'hide_first_embed_video_once', 10 ); remove_filter( 'video_embed_html', 'hide_first_embed_video_once' );

// Re-add with a fresh static context if needed, but this approach is tricky

});

Better yet, instead of static, store a flag in a global variable keyed by post ID, or attach to a post meta or context variable, so you know per-post whether you have hidden a video.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.