functions - Combine embed_oembed_html and oembed_result

admin2025-04-18  0

I have two functions that are exactly the same, because they are applied to different filters...is there any way to combine them so I don't have to write them out twice?

My code:

function embed_oembed($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('embed_oembed_html', 'embed_oembed', 99, 4);


function oembed_result($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('oembed_result', 'oembed_result', 99, 4);

This works, just don't know if it's the proper way to do it:

function oembed_class($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('oembed_result', 'oembed_class', 99, 4);
add_filter('embed_oembed_html', 'oembed_class', 99, 4);

Thanks,
Josh

I have two functions that are exactly the same, because they are applied to different filters...is there any way to combine them so I don't have to write them out twice?

My code:

function embed_oembed($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('embed_oembed_html', 'embed_oembed', 99, 4);


function oembed_result($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('oembed_result', 'oembed_result', 99, 4);

This works, just don't know if it's the proper way to do it:

function oembed_class($html) {
    if (preg_match('/(vimeo)/', $html)) {
        return str_replace('<iframe', '<iframe class="video"', $html);
    } else {
        return $html;
    }
}
add_filter('oembed_result', 'oembed_class', 99, 4);
add_filter('embed_oembed_html', 'oembed_class', 99, 4);

Thanks,
Josh

Share Improve this question asked Nov 25, 2019 at 19:18 joshmrodgjoshmrodg 1,1731 gold badge16 silver badges42 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This works, just don't know if it's the proper way to do it:

Yes, the functions are the same so they're equivalent. There is nothing wrong here. But if we dig a bit deeper, we see they're not 2 filters for the same thing at all:

  • embed_oembed_html filters the cached oEmbed HTML.
  • oembed_result filters the HTML returned by the oEmbed provider.

So oembed_result is when the html is first retrieved, before caching. embed_oembed_html is when the cache has been fetched but before rendering.

You can probably get away with only one or the other, making sure to flush all caches/transients first

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744969926a277424.html

最新回复(0)