函数文档

attachment_submit_meta_box()

💡 云策文档标注

概述

attachment_submit_meta_box() 是 WordPress 核心函数,用于在管理后台的“编辑媒体”屏幕上渲染“附件详情”元框。该元框允许用户查看和修改媒体附件(如图片、视频)的元数据,如上传日期和删除选项。

关键要点

  • 函数接受一个参数 $post,类型为 WP_Post 对象,代表当前正在编辑的媒体附件。
  • 主要功能是显示附件提交表单字段,包括上传日期信息和删除按钮(根据权限和设置显示“移至回收站”或“永久删除”)。
  • 通常无需直接调用此函数,WordPress 核心会在编辑媒体附件时自动处理渲染。
  • 可通过 Hook 如 do_action('attachment_submitbox_misc_actions', $post) 自定义元框内容或行为。

代码示例

function attachment_submit_meta_box( $post ) {
    // 显示上传日期
    $uploaded_on = sprintf(
        __( '%1$s at %2$s' ),
        date_i18n( __( 'F j, Y' ), strtotime( $post->post_date ) ),
        date_i18n( _x( 'H:i', 'publish box time format' ), strtotime( $post->post_date ) )
    );
    printf( __( 'Uploaded on: %s' ), '' . $uploaded_on . '' );

    // 显示删除按钮
    if ( current_user_can( 'delete_post', $post->ID ) ) {
        if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
            printf(
                '%2$s',
                get_delete_post_link( $post->ID ),
                __( 'Move to Trash' )
            );
        } else {
            $show_confirmation = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
            printf(
                '%3$s',
                $show_confirmation,
                get_delete_post_link( $post->ID, '', true ),
                __( 'Delete permanently' )
            );
        }
    }
}

注意事项

  • 此函数自 WordPress 3.5.0 版本引入,用于处理媒体附件的元框渲染。
  • 在自定义开发中,如需修改元框内容,建议使用相关 Hook 而非直接编辑核心函数。
  • 函数内部使用了多个 WordPress 辅助函数,如 date_i18n()、get_delete_post_link() 和 current_user_can(),确保本地化和权限检查。

📄 原文内容

Displays attachment submit form fields.

Parameters

$postWP_Postrequired
Current post object.

Source

function attachment_submit_meta_box( $post ) {
?>
<div class="submitbox" id="submitpost">

<div id="minor-publishing">

<div style="display:none;">

</div>

<div id="misc-publishing-actions">
<div class="misc-pub-section curtime misc-pub-curtime">
<span id="timestamp">
post_date ) ),
/* translators: Publish box time format, see https://www.php.net/manual/datetime.format.php */
date_i18n( _x( 'H:i', 'publish box time format' ), strtotime( $post->post_date ) )
);
/* translators: Attachment information. %s: Date the attachment was uploaded. */
printf( __( 'Uploaded on: %s' ), '<b>' . $uploaded_on . '</b>' );
?>
</span>
</div><!-- .misc-pub-section -->

</div><!-- #misc-publishing-actions -->
<div class="clear"></div>
</div><!-- #minor-publishing -->

<div id="major-publishing-actions">
<div id="delete-action">
ID ) ) {
if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
printf(
'<a class="submitdelete deletion" href="%1$s">%2$s</a>',
get_delete_post_link( $post->ID ),
__( 'Move to Trash' )
);
} else {
$show_confirmation = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';

printf(
'<a class="submitdelete deletion"%1$s href="%2$s">%3$s</a>',
$show_confirmation,
get_delete_post_link( $post->ID, '', true ),
__( 'Delete permanently' )
);
}
}
?>
</div>

<div id="publishing-action">
<span class="spinner"></span>
<input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Update' ); ?>" />
<input name="save" type="submit" class="button button-primary button-large" id="publish" value="<?php esc_attr_e( 'Update' ); ?>" />
</div>
<div class="clear"></div>
</div><!-- #major-publishing-actions -->

</div>

</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/meta-boxes.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/meta-boxes.php#L420">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L420-L501">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/attachment_submitbox_misc_actions/"><span class="hook-func">do_action</span>( ‘attachment_submitbox_misc_actions’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires after the ‘Uploaded on’ section of the Save meta box in the attachment editing screen.</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/submit_button/">submit_button()</a><code>wp-admin/includes/template.php

Echoes a submit button, with provided text and appropriate class(es).

esc_attr_e()wp-includes/l10n.php

Displays translated text that has been escaped for safe use in an attribute.

date_i18n()wp-includes/functions.php

Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.

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

Retrieves the delete posts link for post.

current_user_can()wp-includes/capabilities.php

Returns whether the current user has the specified capability.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_x()wp-includes/l10n.php

Retrieves translated string with gettext context.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

Show 4 moreShow less

Changelog

Version Description
3.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The attachment_submit_meta_box function is a WordPress core function used to render a meta box on the Edit Media screen in the WordPress admin. This meta box allows users to perform actions and make changes related to media attachments (e.g., images, videos) associated with posts or pages.

    Here are the key details about the attachment_submit_meta_box function:

    Parameters: It accepts a single parameter, $post, which is an instance of the WP_Post class representing the media attachment being edited.

    Purpose: This function renders the content of the “Attachment Details” meta box, which is displayed on the Edit Media screen. This meta box contains fields and options for modifying various attributes of the attachment, such as the title, caption, alt text, description, and attachment alignment.

    Usage: Typically, you don’t need to call this function directly in your code. WordPress core handles the rendering of this meta box automatically when you’re editing media attachments through the admin interface.

    Hooks: If you want to customize the behavior or content of this meta box, you can do so by using WordPress hooks and filters, such as add_meta_boxes and save_post. These allow you to add or modify fields and data associated with media attachments.

    In summary, attachment_submit_meta_box is a WordPress core function responsible for rendering the “Attachment Details” meta box in the admin interface, providing a way to edit and manage media attachment attributes.