函数文档

attachment_submitbox_metadata()

💡 云策文档标注

概述

attachment_submitbox_metadata() 函数用于在发布元框中显示附件的不可编辑元数据,包括文件名、尺寸、上传者等信息。它通过获取附件数据并应用过滤器来定制输出。

关键要点

  • 函数显示附件的非可编辑元数据,如文件名、尺寸、上传者、父文章链接和文件大小。
  • 支持音频和视频附件的特定元数据字段,如长度、比特率、格式和编解码器。
  • 使用多个过滤器(如 media_meta、media_submitbox_misc_sections、audio_submitbox_misc_sections)允许开发者自定义元数据输出。
  • 函数内部调用 WordPress 核心函数如 get_post()、wp_get_attachment_metadata() 和 esc_html() 来安全处理数据。

代码示例

// 示例:在自定义上下文中调用函数以显示附件元数据
if ( function_exists('attachment_submitbox_metadata') ) {
    attachment_submitbox_metadata();
}

注意事项

  • 此函数主要用于管理后台的发布元框,不建议在前端直接使用。
  • 元数据输出经过转义处理以确保安全性,开发者应避免绕过这些安全措施。
  • 过滤器可用于扩展或修改显示的元数据字段,需遵循 WordPress 钩子规范。

📄 原文内容

Displays non-editable attachment metadata in the publish meta box.

Source

function attachment_submitbox_metadata() {
$post = get_post();
$attachment_id = $post->ID;

$file = get_attached_file( $attachment_id );
$filename = esc_html( wp_basename( $file ) );

$media_dims = '';
$meta = wp_get_attachment_metadata( $attachment_id );

if ( isset( $meta['width'], $meta['height'] ) ) {
/* translators: 1: A number of pixels wide, 2: A number of pixels tall. */
$media_dims .= "<span id='media-dims-$attachment_id'>" . sprintf( __( '%1$s by %2$s pixels' ), $meta['width'], $meta['height'] ) . '</span>';
}
/** This filter is documented in wp-admin/includes/media.php */
$media_dims = apply_filters( 'media_meta', $media_dims, $post );

$att_url = wp_get_attachment_url( $attachment_id );

$author = new WP_User( $post->post_author );

$uploaded_by_name = __( '(no author)' );
$uploaded_by_link = '';

if ( $author->exists() ) {
$uploaded_by_name = $author->display_name ? $author->display_name : $author->nickname;
$uploaded_by_link = get_edit_user_link( $author->ID );
}
?>
<div class="misc-pub-section misc-pub-uploadedby word-wrap-break-word">

<a href="<?php echo $uploaded_by_link; ?>"><strong></strong></a>

<strong></strong>

</div>

post_parent ) {
$post_parent = get_post( $post->post_parent );
if ( $post_parent ) {
$uploaded_to_title = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' );
$uploaded_to_link = get_edit_post_link( $post->post_parent, 'raw' );
?>
<div class="misc-pub-section misc-pub-uploadedto">

<a href="<?php echo $uploaded_to_link; ?>"><strong></strong></a>

<strong></strong>

</div>

<div class="misc-pub-section misc-pub-attachment">
<label for="attachment_url"></label>
<input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
<span class="copy-to-clipboard-container">
<button type="button" class="button copy-attachment-url edit-media" data-clipboard-target="#attachment_url"></button>
<span class="success hidden" aria-hidden="true"></span>
</span>
</div>

<div class="misc-pub-section misc-pub-download">
<a href="<?php echo esc_attr( $att_url ); ?>" download></a>
</div>

<div class="misc-pub-section misc-pub-filename">
<strong></strong>
</div>

<div class="misc-pub-section misc-pub-filetype">

<strong>
ID ), $matches ) ) {
echo esc_html( strtoupper( $matches[1] ) );
list( $mime_type ) = explode( '/', $post->post_mime_type );
if ( 'image' !== $mime_type && ! empty( $meta['mime_type'] ) ) {
if ( "$mime_type/" . strtolower( $matches[1] ) !== $meta['mime_type'] ) {
echo ' (' . $meta['mime_type'] . ')';
}
}
} else {
echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
}

?>
</strong>
</div>

<div class="misc-pub-section misc-pub-filesize">
<strong></strong>
</div>

post_mime_type ) ) {
$fields = array(
'length_formatted' => __( 'Length:' ),
'bitrate' => __( 'Bitrate:' ),
);

/**
* Filters the audio and video metadata fields to be shown in the publish meta box.
*
* The key for each item in the array should correspond to an attachment
* metadata key, and the value should be the desired label.
*
* @since 3.7.0
* @since 4.9.0 Added the `$post` parameter.
*
* @param array $fields An array of the attachment metadata keys and labels.
* @param WP_Post $post WP_Post object for the current attachment.
*/

$fields = apply_filters( 'media_submitbox_misc_sections', $fields, $post );

foreach ( $fields as $key => $label ) {
if ( empty( $meta[ $key ] ) ) {
continue;
}

?>
<div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">

<strong>

</strong>
</div>
__( 'Audio Format:' ),
'codec' => __( 'Audio Codec:' ),
);

/**
* Filters the audio attachment metadata fields to be shown in the publish meta box.
*
* The key for each item in the array should correspond to an attachment
* metadata key, and the value should be the desired label.
*
* @since 3.7.0
* @since 4.9.0 Added the `$post` parameter.
*
* @param array $fields An array of the attachment metadata keys and labels.
* @param WP_Post $post WP_Post object for the current attachment.
*/

$audio_fields = apply_filters( 'audio_submitbox_misc_sections', $fields, $post );

foreach ( $audio_fields as $key => $label ) {
if ( empty( $meta['audio'][ $key ] ) ) {
continue;
}

?>
<div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
<strong></strong>
</div>

<div class="misc-pub-section misc-pub-dimensions">
<strong></strong>
</div>

<div class="misc-pub-section misc-pub-original-image word-wrap-break-word">

<a href="<?php echo esc_url( wp_get_original_image_url( $attachment_id ) ); ?>">
<strong></strong>
</a>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/media.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/media.php#L3305">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/media.php#L3305-L3516">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/audio_submitbox_misc_sections/"><span class="hook-func">apply_filters</span>( ‘audio_submitbox_misc_sections’, <nobr><span class="arg-type">array</span> <span class="arg-name">$fields</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the audio attachment metadata fields to be shown in the publish meta box.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/media_meta/"><span class="hook-func">apply_filters</span>( ‘media_meta’, <nobr><span class="arg-type">string</span> <span class="arg-name">$media_dims</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the media metadata.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/media_submitbox_misc_sections/"><span class="hook-func">apply_filters</span>( ‘media_submitbox_misc_sections’, <nobr><span class="arg-type">array</span> <span class="arg-name">$fields</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the audio and video metadata fields to be shown in the publish meta box.</p>
</dd></dl></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_filesize/">wp_filesize()</a><code>wp-includes/functions.php

Wrapper for PHP filesize with filters and casting the result as an integer.

wp_get_original_image_url()wp-includes/post.php

Retrieves the URL to an original attachment image.

wp_get_original_image_path()wp-includes/post.php

Retrieves the path to an uploaded image file.

human_readable_duration()wp-includes/functions.php

Converts a duration to human readable format.

WP_User::__construct()wp-includes/class-wp-user.php

Constructor.

sanitize_html_class()wp-includes/formatting.php

Sanitizes an HTML classname to ensure it only contains valid characters.

size_format()wp-includes/functions.php

Converts a number of bytes to the largest unit the bytes will fit into.

get_edit_user_link()wp-includes/link-template.php

Retrieves the edit user link.

get_edit_post_link()wp-includes/link-template.php

Retrieves the edit post link for post.

wp_get_attachment_metadata()wp-includes/post.php

Retrieves attachment metadata for attachment ID.

wp_get_attachment_url()wp-includes/post.php

Retrieves the URL for an attachment.

get_attached_file()wp-includes/post.php

Retrieves attached file path based on attachment ID.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

wp_basename()wp-includes/formatting.php

i18n-friendly version of basename().

esc_html()wp-includes/formatting.php

Escaping for HTML blocks.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

apply_filters()wp-includes/plugin.php

Calls the callback functions that have been added to a filter hook.

get_post()wp-includes/post.php

Retrieves post data given a post ID or post object.

Show 15 moreShow less

Changelog

Version Description
3.5.0 Introduced.