函数文档

post_custom_meta_box()

💡 云策文档标注

概述

post_custom_meta_box() 函数用于在 WordPress 后台编辑文章页面显示自定义字段表单。它处理当前文章的元数据,过滤受保护或用户无权限的字段,并输出相关表单和帮助信息。

关键要点

  • 函数接受一个 WP_Post 对象作为必需参数,表示当前文章。
  • 内部调用 get_post_custom() 获取元数据,并使用 is_protected_meta() 和 current_user_can() 进行权限检查。
  • 通过 list_meta() 和 meta_form() 函数分别输出元数据列表和表单。
  • 包含帮助文本,链接到 WordPress 官方文档。
  • 自 WordPress 2.6.0 版本引入。

代码示例

function post_custom_meta_box( $post ) {
    $metadata = get_post_custom( $post->ID );
    foreach ( $metadata as $key => $value ) {
        if ( is_protected_meta( $metadata[ $key ]['meta_key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post->ID, $metadata[ $key ]['meta_key'] ) ) {
            unset( $metadata[ $key ] );
        }
    }
    list_meta( $metadata );
    meta_form( $post );
}

注意事项

  • 函数主要用于后台管理界面,开发者应确保在正确上下文中调用。
  • 相关函数包括 list_meta()、meta_form()、has_meta()、is_protected_meta()、current_user_can() 和 __()。

📄 原文内容

Displays custom fields form fields.

Parameters

$postWP_Postrequired
Current post object.

Source

function post_custom_meta_box( $post ) {
?>
<div id="postcustomstuff">
<div id="ajax-response"></div>
ID );
foreach ( $metadata as $key => $value ) {
if ( is_protected_meta( $metadata[ $key ]['meta_key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post->ID, $metadata[ $key ]['meta_key'] ) ) {
unset( $metadata[ $key ] );
}
}
list_meta( $metadata );
meta_form( $post );
?>
</div>
<p>
use in your theme</a>.' ),
__( 'https://wordpress.org/documentation/article/assign-custom-fields/' )
);
?>
</p>
</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#L813">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L813-L838">View on GitHub</a></p></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/list_meta/">list_meta()</a><code>wp-admin/includes/template.php

Outputs a post’s public meta data in the Custom Fields meta box.

meta_form()wp-admin/includes/template.php

Prints the form in the Custom Fields meta box.

has_meta()wp-admin/includes/post.php

Returns meta data for the given post ID.

is_protected_meta()wp-includes/meta.php

Determines whether a meta key is considered protected.

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.

Show 2 moreShow less

Changelog

Version Description
2.6.0 Introduced.