get_media_states()
云策文档标注
概述
get_media_states() 函数用于从附件中检索媒体状态数组,主要应用于 WordPress 媒体列表表格中显示附件的状态标签。它检查附件是否被用作自定义页眉、背景、站点图标或徽标,并返回相应的状态数组。
关键要点
- 函数接受一个 WP_Post 对象作为参数,表示要检索状态的附件。
- 返回一个字符串数组,键为状态,值为状态标签(如“Header Image”、“Background Image”)。
- 支持自定义页眉、背景、站点图标和徽标的状态检测,包括当前和历史状态。
- 通过 apply_filters('display_media_states', $media_states, $post) 钩子允许开发者过滤媒体状态。
代码示例
function get_media_states( $post ) {
static $header_images;
$media_states = array();
$stylesheet = get_option( 'stylesheet' );
if ( current_theme_supports( 'custom-header' ) ) {
$meta_header = get_post_meta( $post->ID, '_wp_attachment_is_custom_header', true );
if ( is_random_header_image() ) {
if ( ! isset( $header_images ) ) {
$header_images = wp_list_pluck( get_uploaded_header_images(), 'attachment_id' );
}
if ( $meta_header === $stylesheet && in_array( $post->ID, $header_images, true ) ) {
$media_states[] = __( 'Header Image' );
}
} else {
$header_image = get_header_image();
if ( ! empty( $meta_header ) && $meta_header === $stylesheet && wp_get_attachment_url( $post->ID ) !== $header_image ) {
$media_states[] = __( 'Header Image' );
}
if ( $header_image && wp_get_attachment_url( $post->ID ) === $header_image ) {
$media_states[] = __( 'Current Header Image' );
}
}
if ( get_theme_support( 'custom-header', 'video' ) && has_header_video() ) {
$mods = get_theme_mods();
if ( isset( $mods['header_video'] ) && $post->ID === $mods['header_video'] ) {
$media_states[] = __( 'Current Header Video' );
}
}
}
if ( current_theme_supports( 'custom-background' ) ) {
$meta_background = get_post_meta( $post->ID, '_wp_attachment_is_custom_background', true );
if ( ! empty( $meta_background ) && $meta_background === $stylesheet ) {
$media_states[] = __( 'Background Image' );
$background_image = get_background_image();
if ( $background_image && wp_get_attachment_url( $post->ID ) === $background_image ) {
$media_states[] = __( 'Current Background Image' );
}
}
}
if ( (int) get_option( 'site_icon' ) === $post->ID ) {
$media_states[] = __( 'Site Icon' );
}
if ( (int) get_theme_mod( 'custom_logo' ) === $post->ID ) {
$media_states[] = __( 'Logo' );
}
return apply_filters( 'display_media_states', $media_states, $post );
}注意事项
- 函数内部使用静态变量 $header_images 缓存上传的页眉图像 ID,以提高性能。
- 状态检测依赖于主题支持(如 custom-header、custom-background)和元数据(如 _wp_attachment_is_custom_header)。
- 返回的数组可通过 display_media_states 过滤器进行自定义修改。
- 函数自 WordPress 5.6.0 版本引入。
原文内容
Retrieves an array of media states from an attachment.
Parameters
$postWP_Postrequired-
The attachment to retrieve states for.
Source
function get_media_states( $post ) {
static $header_images;
$media_states = array();
$stylesheet = get_option( 'stylesheet' );
if ( current_theme_supports( 'custom-header' ) ) {
$meta_header = get_post_meta( $post->ID, '_wp_attachment_is_custom_header', true );
if ( is_random_header_image() ) {
if ( ! isset( $header_images ) ) {
$header_images = wp_list_pluck( get_uploaded_header_images(), 'attachment_id' );
}
if ( $meta_header === $stylesheet && in_array( $post->ID, $header_images, true ) ) {
$media_states[] = __( 'Header Image' );
}
} else {
$header_image = get_header_image();
// Display "Header Image" if the image was ever used as a header image.
if ( ! empty( $meta_header ) && $meta_header === $stylesheet && wp_get_attachment_url( $post->ID ) !== $header_image ) {
$media_states[] = __( 'Header Image' );
}
// Display "Current Header Image" if the image is currently the header image.
if ( $header_image && wp_get_attachment_url( $post->ID ) === $header_image ) {
$media_states[] = __( 'Current Header Image' );
}
}
if ( get_theme_support( 'custom-header', 'video' ) && has_header_video() ) {
$mods = get_theme_mods();
if ( isset( $mods['header_video'] ) && $post->ID === $mods['header_video'] ) {
$media_states[] = __( 'Current Header Video' );
}
}
}
if ( current_theme_supports( 'custom-background' ) ) {
$meta_background = get_post_meta( $post->ID, '_wp_attachment_is_custom_background', true );
if ( ! empty( $meta_background ) && $meta_background === $stylesheet ) {
$media_states[] = __( 'Background Image' );
$background_image = get_background_image();
if ( $background_image && wp_get_attachment_url( $post->ID ) === $background_image ) {
$media_states[] = __( 'Current Background Image' );
}
}
}
if ( (int) get_option( 'site_icon' ) === $post->ID ) {
$media_states[] = __( 'Site Icon' );
}
if ( (int) get_theme_mod( 'custom_logo' ) === $post->ID ) {
$media_states[] = __( 'Logo' );
}
/**
* Filters the default media display states for items in the Media list table.
*
* @since 3.2.0
* @since 4.8.0 Added the `$post` parameter.
*
* @param string[] $media_states An array of media states. Default 'Header Image',
* 'Background Image', 'Site Icon', 'Logo'.
* @param WP_Post $post The current attachment object.
*/
return apply_filters( 'display_media_states', $media_states, $post );
}
Hooks
- apply_filters( ‘display_media_states’, string[] $media_states, WP_Post $post )
-
Filters the default media display states for items in the Media list table.
Changelog
| Version | Description |
|---|---|
| 5.6.0 | Introduced. |