5

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.

  • 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 Sep 15 '16 at 13:51
  • I have just tried that. It hasn't made any difference. – danny471 Sep 15 '16 at 14:03
  • 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 Feb 2 at 13:28

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.