函数文档

post_tags_meta_box()

💡 云策文档标注

概述

post_tags_meta_box() 函数用于在 WordPress 后台显示文章标签的编辑表单字段,支持自定义分类法参数。它处理用户权限、术语编辑和界面元素,是标签管理功能的核心部分。

关键要点

  • 函数接受两个参数:$post(WP_Post 对象,必需)和 $box(数组,包含元框参数如 id、title、callback、args 和 taxonomy)。
  • 默认使用 'post_tag' 分类法,但可通过 $box['args'] 自定义,使用 wp_parse_args() 合并参数。
  • 检查当前用户是否有 assign_terms 权限,控制表单的可编辑性。
  • 使用 get_terms_to_edit() 获取可编辑的术语列表,并输出 HTML 表单字段,包括文本区域和输入框。
  • 相关函数包括 get_taxonomy()、current_user_can()、esc_attr() 等,用于增强安全性和功能。

代码示例

function post_tags_meta_box( $post, $box ) {
    $defaults = array( 'taxonomy' => 'post_tag' );
    if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
        $args = array();
    } else {
        $args = $box['args'];
    }
    $parsed_args           = wp_parse_args( $args, $defaults );
    $tax_name              = esc_attr( $parsed_args['taxonomy'] );
    $taxonomy              = get_taxonomy( $parsed_args['taxonomy'] );
    $user_can_assign_terms = current_user_can( $taxonomy->cap->assign_terms );
    $comma                 = _x( ',', 'tag delimiter' );
    $terms_to_edit         = get_terms_to_edit( $post->ID, $tax_name );
    if ( ! is_string( $terms_to_edit ) ) {
        $terms_to_edit = '';
    }
    // 输出 HTML 表单代码
}

注意事项

  • 确保 $post 参数是有效的 WP_Post 对象,否则可能导致错误。
  • 自定义分类法时,需在 $box['args'] 中正确设置 taxonomy 参数。
  • 用户权限检查基于 current_user_can(),需确保分类法能力设置正确。
  • 函数输出 HTML 包含 aria 属性和翻译文本,以提升可访问性和国际化。
  • 自 WordPress 2.6.0 版本引入,兼容性良好。

📄 原文内容

Displays post tags form fields.

Parameters

$postWP_Postrequired
Current post object.
$boxarrayrequired
Tags meta box arguments.
  • id string
    Meta box 'id' attribute.
  • title string
    Meta box title.
  • callback callable
    Meta box display callback.
  • args array
    Extra meta box arguments.
    • taxonomy string
      Taxonomy. Default 'post_tag'.

Source

function post_tags_meta_box( $post, $box ) {
	$defaults = array( 'taxonomy' => 'post_tag' );
	if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
		$args = array();
	} else {
		$args = $box['args'];
	}
	$parsed_args           = wp_parse_args( $args, $defaults );
	$tax_name              = esc_attr( $parsed_args['taxonomy'] );
	$taxonomy              = get_taxonomy( $parsed_args['taxonomy'] );
	$user_can_assign_terms = current_user_can( $taxonomy->cap->assign_terms );
	$comma                 = _x( ',', 'tag delimiter' );
	$terms_to_edit         = get_terms_to_edit( $post->ID, $tax_name );
	if ( ! is_string( $terms_to_edit ) ) {
		$terms_to_edit = '';
	}
	?>
<div class="tagsdiv" id="<?php echo $tax_name; ?>">
	<div class="jaxtag">
	<div class="nojs-tags hide-if-js">
		<label for="tax-input-<?php echo $tax_name; ?>">labels->add_or_remove_items; ?></label>
		<p><textarea name="<?php echo "tax_input[$tax_name]"; ?>" rows="3" cols="20" class="the-tags" id="tax-input-"  aria-describedby="new-tag--desc"></textarea></p>
	</div>
	
	<div class="ajaxtag hide-if-no-js">
		<label class="screen-reader-text" for="new-tag-<?php echo $tax_name; ?>">labels->add_new_item; ?></label>
		<input data-wp-taxonomy="<?php echo $tax_name; ?>" type="text" id="new-tag-<?php echo $tax_name; ?>" name="newtag[<?php echo $tax_name; ?>]" class="newtag form-input-tip" size="16" autocomplete="off" aria-describedby="new-tag-<?php echo $tax_name; ?>-desc" value="" />
		<input type="button" class="button tagadd" value="<?php esc_attr_e( 'Add' ); ?>" />
	</div>
	<p class="howto" id="new-tag-<?php echo $tax_name; ?>-desc">labels->separate_items_with_commas; ?></p>
	
		<p>labels->no_terms; ?></p>
	
	</div>
	<ul class="tagchecklist" role="list"></ul>
</div>
	
<p class="hide-if-no-js"><button type="button" class="button-link tagcloud-link" id="link-<?php echo $tax_name; ?>" aria-expanded="false">labels->choose_from_most_used; ?></button></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#L572">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L572-L612">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/get_terms_to_edit/">get_terms_to_edit()</a><code>wp-admin/includes/taxonomy.php

Gets comma-separated list of terms available to edit for the given post ID.

esc_attr_e()wp-includes/l10n.php

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

disabled()wp-includes/general-template.php

Outputs the HTML disabled attribute.

current_user_can()wp-includes/capabilities.php

Returns whether the current user has the specified capability.

_x()wp-includes/l10n.php

Retrieves translated string with gettext context.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

wp_parse_args()wp-includes/functions.php

Merges user defined arguments into defaults array.

get_taxonomy()wp-includes/taxonomy.php

Retrieves the taxonomy object of $taxonomy.

Show 5 moreShow less

Changelog

VersionDescription
2.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.