钩子文档

oembed_dataparse

💡 云策文档标注

概述

oembed_dataparse 是一个 WordPress 过滤器,用于修改或处理从 oEmbed 提供者返回的 HTML 内容。开发者可以利用此过滤器添加对自定义数据类型的支持,或过滤嵌入结果。

关键要点

  • 过滤器名称:oembed_dataparse,用于过滤返回的 oEmbed HTML。
  • 参数:$return(返回的 HTML 字符串或 false)、$data(oEmbed 提供者的数据对象)、$url(要嵌入内容的 URL)。
  • 应用场景:支持自定义数据类型、包装响应 HTML(如基于类型和尺寸添加 CSS 类)、清除 oEmbed 缓存以首次使用。

代码示例

function wrap_oembed_dataparse($return, $data, $url) {
    $mod = '';
    if ( ( $data->type == 'video' ) &&
         ( isset($data->width) ) && ( isset($data->height) ) &&
         ( round($data->height/$data->width, 2) == round( 3/4, 2) )
    ) {
        $mod = 'embed-responsive--4-3';
    }
    return '<div class="embed-responsive ' . $mod . '">' . $return . '</div>';
}
add_filter( 'oembed_dataparse', 'wrap_oembed_dataparse', 99, 4 );

注意事项

首次使用 oembed_dataparse 时,可能需要清除 oEmbed 的 post-meta 缓存,可通过修改 oembed_ttl 和 embed_oembed_discover 过滤器实现。


📄 原文内容

Filters the returned oEmbed HTML.

Description

Use this filter to add support for custom data types, or to filter the result.

Parameters

$returnstring|false
The returned oEmbed HTML, or false on failure.
$dataobject
A data object result from an oEmbed provider.
$urlstring
The URL of the content to be embedded.

Source

return apply_filters( 'oembed_dataparse', $return, $data, $url );

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Can be used to wrap oEmbed response in html, depending on type and size of oEmbed.

    function wrap_oembed_dataparse($return, $data, $url) {
    
    	$mod = '';
    
    	if  (   ( $data->type == 'video' ) &&
    			( isset($data->width) ) && ( isset($data->height) ) &&
    			( round($data->height/$data->width, 2) == round( 3/4, 2) )
    		)
    	{
    		$mod = 'embed-responsive--4-3';
    	}
    
    	return '<div class="embed-responsive ' . $mod . '">' . $return . '</div>';
    }
    
    add_filter( 'oembed_dataparse', 'wrap_oembed_dataparse', 99, 4 );

    To use ‘oembed_dataparse’ for the first time, you might have to clear oEmbed post-meta cache:

    add_filter( 'oembed_ttl', function($ttl) {
    	  $GLOBALS['wp_embed']->usecache = 0;
                $ttl = 0;
                // House-cleanoing
                do_action( 'wpse_do_cleanup' );
    	return $ttl;
    });
    
    add_filter( 'embed_oembed_discover', function( $discover )
    {
        if( 1 === did_action( 'wpse_do_cleanup' ) )
            $GLOBALS['wp_embed']->usecache = 1;
        return $discover;
    } );