embed_defaults
云策文档标注
概述
embed_defaults 过滤器用于修改 WordPress 中嵌入内容(如视频、图片)的默认尺寸数组。开发者可以通过此 Hook 自定义嵌入的宽度和高度,以适应主题或特定需求。
关键要点
- 过滤器名称:embed_defaults,用于过滤嵌入尺寸的默认数组。
- 参数:接受两个参数,一个包含宽度和高度的索引数组,以及嵌入的 URL 字符串。
- 默认行为:如果主题未指定内容宽度,则使用 500px 宽度,高度为宽度的 1.5 倍或 1000px 中的较小值。
- 回调函数必须返回一个包含 'width' 和 'height' 键的数组。
代码示例
add_filter( 'embed_defaults', 'modify_embed_defaults' );
function modify_embed_defaults() {
return array(
'width' => 750,
'height' => 375
);
}注意事项
此过滤器自 WordPress 2.9.0 版本引入,常用于子主题的 functions.php 文件中进行自定义设置。
原文内容
Filters the default array of embed dimensions.
Parameters
$sizeint[]-
Indexed array of the embed width and height in pixels.
0intThe embed width.1intThe embed height.
$urlstring-
The URL that should be embedded.
Source
return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url );
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
Skip to note 2 content
Steven Lin
Example migrated from Codex:
Modify the default embed dimensions by adding the filter to the
functions.phpfile of a child theme.add_filter( 'embed_defaults', 'modify_embed_defaults' ); function modify_embed_defaults() { return array( 'width' => 750, 'height' => 375 ); }