函数文档

get_attachment_fields_to_edit()

💡 云策文档标注

概述

get_attachment_fields_to_edit() 函数用于检索附件编辑表单字段,返回一个包含字段配置的数组。它处理附件的基本信息、元数据和分类法,并支持通过过滤器进行自定义。

关键要点

  • 函数接受两个参数:$post(必需,WP_Post 对象或 ID)和 $errors(可选,数组,默认为 null),用于合并错误信息。
  • 返回一个数组,包含附件编辑表单的字段定义,如标题、替代文本、描述、链接 URL、排序等。
  • 自动处理附件分类法,仅包含公开且显示在 UI 中的分类法,并获取相关术语。
  • 对于图像附件,会添加特定字段如替代文本、对齐方式和图像尺寸选项;非图像附件则移除替代文本字段。
  • 使用 apply_filters('attachment_fields_to_edit', $form_fields, $post) 钩子允许开发者过滤字段。

代码示例

function get_attachment_fields_to_edit( $post, $errors = null ) {
    // 函数实现代码,包括参数处理、字段数组构建和过滤器应用。
}

注意事项

  • 函数内部使用 wp_get_attachment_url()、sanitize_post() 等辅助函数确保数据安全。
  • 错误参数通过 array_merge_recursive() 合并到字段数组中,便于覆盖默认值。
  • 仅适用于附件编辑场景,如媒体库中的修改界面。

📄 原文内容

Retrieves the attachment fields to edit form fields.

Parameters

$postWP_Postrequired
$errorsarrayoptional

Default:null

Return

array

Source

function get_attachment_fields_to_edit( $post, $errors = null ) {
	if ( is_int( $post ) ) {
		$post = get_post( $post );
	}

	if ( is_array( $post ) ) {
		$post = new WP_Post( (object) $post );
	}

	$image_url = wp_get_attachment_url( $post->ID );

	$edit_post = sanitize_post( $post, 'edit' );

	$form_fields = array(
		'post_title'   => array(
			'label' => __( 'Title' ),
			'value' => $edit_post->post_title,
		),
		'image_alt'    => array(),
		'post_excerpt' => array(
			'label' => __( 'Caption' ),
			'input' => 'html',
			'html'  => wp_caption_input_textarea( $edit_post ),
		),
		'post_content' => array(
			'label' => __( 'Description' ),
			'value' => $edit_post->post_content,
			'input' => 'textarea',
		),
		'url'          => array(
			'label' => __( 'Link URL' ),
			'input' => 'html',
			'html'  => image_link_input_fields( $post, get_option( 'image_default_link_type' ) ),
			'helps' => __( 'Enter a link URL or click above for presets.' ),
		),
		'menu_order'   => array(
			'label' => __( 'Order' ),
			'value' => $edit_post->menu_order,
		),
		'image_url'    => array(
			'label' => __( 'File URL' ),
			'input' => 'html',
			'html'  => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr( $image_url ) . "' /><br />",
			'value' => wp_get_attachment_url( $post->ID ),
			'helps' => __( 'Location of the uploaded file.' ),
		),
	);

	foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
		$t = (array) get_taxonomy( $taxonomy );

		if ( ! $t['public'] || ! $t['show_ui'] ) {
			continue;
		}

		if ( empty( $t['label'] ) ) {
			$t['label'] = $taxonomy;
		}

		if ( empty( $t['args'] ) ) {
			$t['args'] = array();
		}

		$terms = get_object_term_cache( $post->ID, $taxonomy );

		if ( false === $terms ) {
			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
		}

		$values = array();

		foreach ( $terms as $term ) {
			$values[] = $term->slug;
		}

		$t['value'] = implode( ', ', $values );

		$form_fields[ $taxonomy ] = $t;
	}

	/*
	 * Merge default fields with their errors, so any key passed with the error
	 * (e.g. 'error', 'helps', 'value') will replace the default.
	 * The recursive merge is easily traversed with array casting:
	 * foreach ( (array) $things as $thing )
	 */
	$form_fields = array_merge_recursive( $form_fields, (array) $errors );

	// This was formerly in image_attachment_fields_to_edit().
	if ( str_starts_with( $post->post_mime_type, 'image' ) ) {
		$alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );

		if ( empty( $alt ) ) {
			$alt = '';
		}

		$form_fields['post_title']['required'] = true;

		$form_fields['image_alt'] = array(
			'value' => $alt,
			'label' => __( 'Alternative Text' ),
			'helps' => __( 'Alt text for the image, e.g. “The Mona Lisa”' ),
		);

		$form_fields['align'] = array(
			'label' => __( 'Alignment' ),
			'input' => 'html',
			'html'  => image_align_input_fields( $post, get_option( 'image_default_align' ) ),
		);

		$form_fields['image-size'] = image_size_input_fields( $post, get_option( 'image_default_size', 'medium' ) );

	} else {
		unset( $form_fields['image_alt'] );
	}

	/**
	 * Filters the attachment fields to edit.
	 *
	 * @since 2.5.0
	 *
	 * @param array   $form_fields An array of attachment form fields.
	 * @param WP_Post $post        The WP_Post attachment object.
	 */
	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );

	return $form_fields;
}

Hooks

apply_filters( ‘attachment_fields_to_edit’, array $form_fields, WP_Post $post )

Filters the attachment fields to edit.

Changelog

Version Description
2.5.0 Introduced.