embed_oembed_htmlхук-фильтрWP 2.9.0

Позволяет изменить закешированный HTML oEmbed при выводе на экран.

Что такое oEmbed читайте здесь: oEmbed в WordPress

За создание метаданных отвечает метод WP_Embed::shortcode().

Использование

add_filter( 'embed_oembed_html', 'wp_kama_embed_oembed_html_filter', 10, 4 );

/**
 * Function for `embed_oembed_html` filter-hook.
 * 
 * @param string|false $cache   The cached HTML result, stored in post meta.
 * @param string       $url     The attempted embed URL.
 * @param array        $attr    An array of shortcode attributes.
 * @param int          $post_id Post ID.
 *
 * @return string|false
 */
function wp_kama_embed_oembed_html_filter( $cache, $url, $attr, $post_id ){

	// filter...
	return $cache;
}
$cache(строка/false)
Закешированный HTML ответа, хранящийся в метаданных записи.
$url(строка)
Ссылка на запрашиваемую сущность (YouTube ролик и т.д.).
$attr(массив)
Массив с атрибутами шорткода (предустановками).
$post_ID(число)
ID записи.

Примеры

1

#1 Добавим обёртку всем oEmbed

add_filter( 'embed_oembed_html', 'add_my_wrapper_all_oembed' );

function add_my_wrapper_all_oembed( $cache ) {
	return sprintf( '<div class="my-class-wrapper">%s</div>', $cache );
}
0

#2 Добавим обёртку только для YouTube плеера

add_filter( 'embed_oembed_html', 'add_youtube_wrap_oembed', 10, 2 );

function add_youtube_wrap_oembed( $cached, $url ) {
	if ( false !== strpos( $url, "://youtube.com" ) || false !== strpos( $url, "://youtu.be" ) ) {
		return sprintf( '<div class="youtube-wrapper">%s</div>', $cached );
	}

	return $cached;
}

Список изменений

С версии 2.9.0 Введена.

Где вызывается хук

WP_Embed::shortcode()
embed_oembed_html
wp-includes/class-wp-embed.php 291
return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_id );
wp-includes/class-wp-embed.php 374
return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_id );

Где используется хук в WordPress

wp-includes/default-filters.php 682
add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );