Вопрос

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.com)/', $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.com)/', $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.com)/', $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

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top