WordPress как на ладони
wordpress jino

attachment_fields_to_edit хук-фильтр . WP 2.5.0

Позволяет добавить произвольное поле к изображению (html код поля на странице редактирования вложения).

Этот хук только добавляет метаполе, но не сохраняет его данные. Для сохранения используйте хук attachment_fields_to_save.

Использование

add_filter( 'attachment_fields_to_edit', 'filter_function_name_5295', 10, 2 );
function filter_function_name_5295( $form_fields, $post ){
	// filter...

	return $form_fields;
}
$form_fields(массив)
Данные полей формы вложения.
$post(WP_Post)
Объект записи вложения.

Примеры

#1 Добавим метаполе для изображения

// Добавим метаполе для изображения
add_filter( 'attachment_fields_to_edit', 'pon_attachment_fields_to_edit', null, 2 );
function pon_attachment_fields_to_edit( $form_fields, $post ){

	$form_fields['carousel_price'] = array(
		'label' => 'Цена (если нужно)',
		'input' => '',
		'value' => get_post_meta( $post->ID, 'carousel_price', true )
	);

	return $form_fields;
}

// Сохраняем данные метаполя
add_filter("attachment_fields_to_save", "pon_attachment_fields_to_save", null, 2);
function pon_attachment_fields_to_save($post, $attachment) {
	if( isset($attachment['carousel_price']) ){
		update_post_meta( $post['ID'], 'carousel_price', $attachment['carousel_price'] );
	}
	else
		delete_post_meta( $post['ID'], 'carousel_price' );

	return $post;
}

Получим:

Связи хука

Используется в: get_attachment_fields_to_edit(), get_compat_media_markup()

Остальные хуки из: get_compat_media_markup():

Фрагменты кода хука attachment_fields_to_edit

Фрагмент из: wp-admin/includes/media.php WP 4.9
...

		$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;
}

/**
 * Retrieve HTML for media items of post gallery.
 *
 * The HTML markup retrieved will be created for the progress of SWF Upload
 * component. Will also create link for showing and hiding the form to modify
 * the image attachment.
 *
 * @since 2.5.0
 *
 * @global WP_Query $wp_the_query
 *
...
Фрагмент из: wp-admin/includes/media.php WP 4.9
...

			foreach ( $terms as $term )
				$values[] = $term->slug;
			$t['value'] = join(', ', $values);
			$t['taxonomy'] = true;

			$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) $args['errors'] );

	/** This filter is documented in wp-admin/includes/media.php */
	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );

	unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
		$form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
		$form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );

	/** This filter is documented in wp-admin/includes/media.php */
	$media_meta = apply_filters( 'media_meta', '', $post );

	$defaults = array(
		'input'         => 'text',
		'required'      => false,
		'value'         => '',
		'extra_rows'    => array(),
		'show_in_edit'  => true,
		'show_in_modal' => true,
...
attachment_fields_to_edit Комментариев нет

Здравствуйте, !

Ваш комментарий