我想为embed\\u oembed\\u html过滤器(iframe)添加一个自定义类。
注1:使用str\\u replace将删除其他类,因此它没有用处。注2:我需要将自定义类直接添加到ifram标记中,而不是将完整的iframe代码添加到另一个标记中,如<div>
.
我想为embed\\u oembed\\u html过滤器(iframe)添加一个自定义类。
注1:使用str\\u replace将删除其他类,因此它没有用处。注2:我需要将自定义类直接添加到ifram标记中,而不是将完整的iframe代码添加到另一个标记中,如<div>
.
你不能仅仅添加一个类,因为WordPress不知道任何关于现有类的信息,也不能像发布和导航项目那样控制现有类。这是因为HTML来自第三方raw。
所以要替换您需要使用的类str_replace()
. 您可以使用它添加类或任何文本,方法如下:
$cache = str_replace( \'<iframe class="existing-class \', \'<iframe class="existing-class lazy-load \', $cache );
但是这个例子只适用于A.如果嵌入的代码是iframe(并不总是保证)和B。class=""
是标记中的第一个属性。这意味着要替换的确切字符串将根据原始HTML而有所不同,这对于每个提供程序都是不同的。在过滤器中,您需要检查源代码并执行不同的操作str_replace()
要容纳此类需要支持的所有提供程序,请执行以下操作:
function wpse_287229_embed_html( $cache, $url, $attr, $post_ID ) {
if (
strpos( $cache, \'youtube.com\' ) !== false ||
strpos( $cache, \'youtu.be\' ) !== false
) {
$cache = str_replace( \'<iframe \', \'<iframe class="my-class" \', $cache ); // YouTube doesn\'t have a class on its iframe.
}
if ( strpos( $cache, \'soundcloud.com\' ) !== false ) {
// str_replace optimised for SoundCloud here.
}
// etc.
return $cache;
}
add_filter( \'embed_oembed_html\', \'wpse_287229_embed_html\', 10, 4 );
主题检查错误→REQUIRED: get_bloginfo(\'template_url\') was found in the file search.php. Use get_template_directory_uri() instead. Line 21: <img src=\'<?php echo get_bloginfo(\'template_url\') ?>/img/no_results_found.png\'/> 我采取了行动并进行了更改,但该图