admin管理员组

文章数量:1123507

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.

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.

Share Improve this question edited Sep 15, 2016 at 19:17 danny471 asked Sep 15, 2016 at 12:26 danny471danny471 1912 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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;
     }
 }

It sounds like your function is being triggered multiple times on the live server due to differences in how WordPress processes content in that environment. Here are some possible reasons and solutions:

Possible Causes & Solutions:

  1. Theme or Plugin Conflicts:

    • Some themes or plugins may be calling the_content multiple times, causing the filter to execute repeatedly. Try switching to a default theme (e.g., Twenty Twenty-Four) and disabling plugins to check.
  2. Caching Issues:

    • Server-side caching (e.g., Nginx, Varnish, or WP caching plugins) may be affecting how the function runs. Clear all caches and try again.
  3. PHP Version Differences:

    • Your local machine and live server may be running different PHP versions. Check this with:
      phpinfo();
      
      • If the live server is on an older PHP version, consider upgrading.
  4. Multiple Calls to embed_oembed_html:

    • Some themes call embedded content multiple times, especially if using get_the_content() instead of the_content(). Check your theme files for redundant calls.
  5. Server Configuration Differences:

    • The live server may have different PHP or WordPress configurations. Debug logs might help. Add this to wp-config.php:
      define('WP_DEBUG', true);
      define('WP_DEBUG_LOG', true);
      define('WP_DEBUG_DISPLAY', false);
      
      Then, check wp-content/debug.log for errors.

If the issue persists, you might be dealing with a broader WordPress-related error. You can refer to this troubleshooting guide for additional debugging steps, as some server-side issues can cause unexpected behavior.

本文标签: phpstatic variable loop not working in Wordpress