Ok, the answer will not be short
At first, You can't change the alt attribute using attachment_fields_to_edit, because you can't modify default fields. To see how this works can be in the source code of get_compat_media_markup.
With attachment_fields_to_edit
you can add only additional input fields.
Example:
/**
* 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);
For save value, use 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);
What to do with the alt attribute?
No good idea, although it is not good to keep trash in database, but work:
add_attachment and add_post_meta variant fires once when attachment is added:
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');
}
Fired, when any field change:
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);
And other way - add alt text when image inserting in post:
/**
* 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);