好的,答案不会很短
首先,不能使用更改alt属性attachment_fields_to_edit, 因为您无法修改默认字段。要了解其工作原理,请参阅的源代码get_compat_media_markup.
具有attachment_fields_to_edit
只能添加其他输入字段。
示例:
/**
* Add custom field for Images
*
* @param array $fields An array of attachment form fields.
* field arguments:
* array(
* \'show_in_edit\' => (bool) Show in Edit Screen. Default true,
* \'show_in_modal\' => (bool) Show in Modal Screen. Default true,
* \'label\' => string label text,
* \'input\' => string input type: text, textarea, etc or \'html\' key with custom input html callback. Default \'text\'
* \'required\' => (bool) Input attributte \'required\'. Default false,
* \'html\' => string custom input html or callback name,
* \'extra_rows\' => array(),
* \'helps\' => string help text,
* )
* @param WP_Post $post The WP_Post attachment object.
*/
function add_attachment_fields($fields, $post)
{
if (substr($post->post_mime_type, 0, 5) == \'image\') {
$meta = wp_get_attachment_metadata($post->ID);
$fields[\'meta_credit\'] = array(
\'label\' => __(\'Credit\'),
\'input\' => \'text\',
\'value\' => $meta[\'image_meta\'][\'credit\'],
\'helps\' => __(\'Only text. Max length 40 characters\'),
\'error_text\' => __(\'Error credit meta\')
);
}
return $fields;
}
add_filter(\'attachment_fields_to_edit\', \'add_attachment_fields\', 10, 2);
要保存值,请使用
attachment_fields_to_save:
/**
* Filters the attachment fields to be saved.
* @param array $post An array of post data.
* @param array $attachment An array of attachment metadata.
*/
function update_attachment_fields($post, $attachment)
{
if (isset($attachment[\'meta_credit\'])) {
$credit = $attachment[\'meta_credit\'];
$meta = wp_get_attachment_metadata($post[\'ID\']);
if ($credit !== $meta[\'image_meta\'][\'credit\']) {
$meta[\'image_meta\'][\'credit\'] = $credit;
wp_update_attachment_metadata($post[\'ID\'], $meta);
}
}
return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_attachment_fields\', 10, 2);
如何处理alt属性?没什么好主意,虽然在数据库中保留垃圾不太好,但可以:
add_attachment 和add_post_meta 添加附件时,变体会触发一次:
add_action(\'add_attachment\', \'add_alt_to_attachment\');
function add_alt_to_attachment($post_ID)
{
add_post_meta($post_ID, \'_wp_attachment_image_alt\', \'MY TEXT\');
}
任何字段更改时激发:
function update_alt_field($post, $attachment)
{
if (empty($attachment[\'image_alt\'])) {
$image_alt = wp_unslash(\'My Alt is good\');
$image_alt = wp_strip_all_tags($image_alt, true);
// Update_meta expects slashed.
update_post_meta($attachment_id, \'_wp_attachment_image_alt\', wp_slash( $image_alt));
}
return $post;
}
add_filter(\'attachment_fields_to_save\', \'update_alt_field\', 10, 2);
和其他方式-在post中插入图像时添加alt文本:
/**
* Filter the list of attachment image attributes.
*
* @param array $attrs Attributes for the image markup.
* @param WP_Post $attachment Image attachment post.
* @param string|array $size Requested size. Image size or array of width and height values
* (in that order). Default \'thumbnail\'.
* @return array $attrs Attributes for the image markup.
*/
function my_images_attr($attrs, $attachment, $size)
{
if (empty($attrs[\'alt\'])) {
$attrs[\'alt\']) = \'MY TEXT\';
}
return $attrs;
}
add_filter(\'wp_get_attachment_image_attributes\', \'my_images_attr\', 10, 3);