最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Combine embed_oembed_html and oembed_result

programmeradmin0浏览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

发布评论

评论列表(0)

  1. 暂无评论