函数文档

get_inline_data()

💡 云策文档标注

概述

get_inline_data() 函数用于为文章和页面的内联编辑器输出隐藏字段数据。它检查用户权限,并输出包括标题、状态、日期、分类等在内的多种信息,以支持快速编辑功能。

关键要点

  • 函数输出隐藏字段,包含文章或页面的核心数据,如标题、作者、状态、日期、密码、分类等。
  • 权限检查:仅当当前用户具有编辑权限时才会执行输出。
  • 支持分层文章类型(如页面)输出父级ID,非分层类型输出置顶状态。
  • 处理分类法数据,包括分层和非分层分类,并缓存术语以提高性能。
  • 触发 add_inline_data 动作钩子,允许开发者自定义输出。
  • 使用多个辅助函数,如 esc_textarea、mysql2date、get_object_taxonomies 等确保数据安全性和格式正确。

代码示例

注意事项

  • 函数直接输出 HTML,通常用于管理后台的列表页面,如 WP_Posts_List_Table。
  • 确保在调用前已正确设置 $post 参数为 WP_Post 对象。
  • 分类法数据输出依赖于 show_in_quick_edit 属性,需在注册分类法时设置。
  • 使用缓存机制(如 wp_cache_add)优化术语查询性能。

📄 原文内容

Adds hidden fields with the data for use in the inline editor for posts and pages.

Parameters

$postWP_Postrequired
Post object.

Source

function get_inline_data( $post ) {
	$post_type_object = get_post_type_object( $post->post_type );
	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
		return;
	}

	$title = esc_textarea( trim( $post->post_title ) );

	echo '
<div class="hidden" id="inline_' . $post->ID . '">
	<div class="post_title">' . $title . '</div>' .
	/** This filter is documented in wp-admin/edit-tag-form.php */
	'<div class="post_name">' . apply_filters( 'editable_slug', $post->post_name, $post ) . '</div>
	<div class="post_author">' . $post->post_author . '</div>
	<div class="comment_status">' . esc_html( $post->comment_status ) . '</div>
	<div class="ping_status">' . esc_html( $post->ping_status ) . '</div>
	<div class="_status">' . esc_html( $post->post_status ) . '</div>
	<div class="jj">' . mysql2date( 'd', $post->post_date, false ) . '</div>
	<div class="mm">' . mysql2date( 'm', $post->post_date, false ) . '</div>
	<div class="aa">' . mysql2date( 'Y', $post->post_date, false ) . '</div>
	<div class="hh">' . mysql2date( 'H', $post->post_date, false ) . '</div>
	<div class="mn">' . mysql2date( 'i', $post->post_date, false ) . '</div>
	<div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
	<div class="post_password">' . esc_html( $post->post_password ) . '</div>';

	if ( $post_type_object->hierarchical ) {
		echo '<div class="post_parent">' . $post->post_parent . '</div>';
	}

	echo '<div class="page_template">' . ( $post->page_template ? esc_html( $post->page_template ) : 'default' ) . '</div>';

	if ( post_type_supports( $post->post_type, 'page-attributes' ) ) {
		echo '<div class="menu_order">' . $post->menu_order . '</div>';
	}

	$taxonomy_names = get_object_taxonomies( $post->post_type );

	foreach ( $taxonomy_names as $taxonomy_name ) {
		$taxonomy = get_taxonomy( $taxonomy_name );

		if ( ! $taxonomy->show_in_quick_edit ) {
			continue;
		}

		if ( $taxonomy->hierarchical ) {

			$terms = get_object_term_cache( $post->ID, $taxonomy_name );
			if ( false === $terms ) {
				$terms = wp_get_object_terms( $post->ID, $taxonomy_name );
				wp_cache_add( $post->ID, wp_list_pluck( $terms, 'term_id' ), $taxonomy_name . '_relationships' );
			}
			$term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );

			echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode( ',', $term_ids ) . '</div>';

		} else {

			$terms_to_edit = get_terms_to_edit( $post->ID, $taxonomy_name );
			if ( ! is_string( $terms_to_edit ) ) {
				$terms_to_edit = '';
			}

			echo '<div class="tags_input" id="' . $taxonomy_name . '_' . $post->ID . '">'
				. esc_html( str_replace( ',', ', ', $terms_to_edit ) ) . '</div>';

		}
	}

	if ( ! $post_type_object->hierarchical ) {
		echo '<div class="sticky">' . ( is_sticky( $post->ID ) ? 'sticky' : '' ) . '</div>';
	}

	if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
		echo '<div class="post_format">' . esc_html( get_post_format( $post->ID ) ) . '</div>';
	}

	/**
	 * Fires after outputting the fields for the inline editor for posts and pages.
	 *
	 * @since 4.9.8
	 *
	 * @param WP_Post      $post             The current post object.
	 * @param WP_Post_Type $post_type_object The current post's post type object.
	 */
	do_action( 'add_inline_data', $post, $post_type_object );

	echo '</div>';
}

Hooks

do_action( ‘add_inline_data’, WP_Post $post, WP_Post_Type $post_type_object )

Fires after outputting the fields for the inline editor for posts and pages.

apply_filters( ‘editable_slug’, string $slug, WP_Term|WP_Post $tag )

Filters the editable slug for a post or term.

Changelog

Version Description
2.7.0 Introduced.