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

Сохраняет произвольное поле добавленное к изображению (вложению) с помощью хука attachment_fields_to_edit.

Срабатывает автосохранение при загрузке изображения например в записях (проверить, потому что не выводится надпись "Сохраняем...")

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

add_filter( 'attachment_fields_to_save', 'wp_kama_attachment_fields_to_save_filter', 10, 2 );

/**
 * Function for `attachment_fields_to_save` filter-hook.
 * 
 * @param array $post       An array of post data.
 * @param array $attachment An array of attachment metadata.
 *
 * @return array
 */
function wp_kama_attachment_fields_to_save_filter( $post, $attachment ){

	// filter...
	return $post;
}
$post(массив)
Массив данных вложения (записи).
$attachment(массив)
Массив мета-данных вложения (записи).

Примеры

0

#1 Сохраняем данные метаполя

Допустим мы добавили метаполе carousel_price (см. хук attachment_fields_to_edit()).

// Добавим метаполе для изображения
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;
}

Список изменений

С версии 2.5.0 Введена.

Где вызывается хук

media_upload_form_handler()
attachment_fields_to_save
wp_ajax_save_attachment_compat()
attachment_fields_to_save
edit_post()
attachment_fields_to_save
wp-admin/includes/media.php 792
$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
wp-admin/includes/ajax-actions.php 3220
$post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );
wp-admin/includes/post.php 435
$translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data );

Где используется хук в WordPress

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