0

I'd like to add two custom text fields for image attachments to posts (images inserted into the content of a post). Second I'd also like to remote some of the default fields like caption and description.

I've managed to add fields like so:

add_filter('attachment_fields_to_edit', array($this, 'applyFilter'), 1, 2);

public function applyFilter( $form_fields, $post = null ) {

    $form_fields['someCustom'] = array(
        'label' => 'Image credit',
        'input'       => 'text',
        'helps'       => 'Photographer / bureau',
        'application' => 'image',
        'exclusions'  => array( 'audio', 'video' ),
        'required'    => true,
        'error_text'  => 'Credit field required',
    );
    return $form_fields;
}

This adds the field within the media library modal, but only when inserting new images - not when editing existing images. Also the required parameter doesn't seem to prevent inserting images even when the field is empty.

No matter how I set the priority of the filter (tried everything from null, to 1, to 100) it seems to run before the default fields are added to form_fields, making it impossible to remove any default fields.

I'd very much appreciate any help in the matter.

5
  • can't see anything immediately wrong, the filter priority is not the issue though as the default fields are not added via the filter, they should be set regardless. possibly the value field is missing and that could prevent it from showing up when re-editing? hope that helps.
    – majick
    Commented Jul 26, 2016 at 9:57
  • You are correct, in a way. But perhaps this is not at all what I need. What I'd like is for the field(s) to appear both in the media library, as well as when clicking to edit the image in the post content (just like the caption and alt-text fields does). My custom field only shows when adding a new image. Commented Jul 26, 2016 at 11:54
  • yes i am guessing somewhat at the moment I admit but I was thinking that if you don't check/get/set the value field for an initial saved value it could be the reason for it not showing up later on the edit screen?
    – majick
    Commented Jul 26, 2016 at 12:30
  • Yep, but it does not seem to be the way. Also there is still nothing from keeping me to insert images into the post without the required custom field. Commented Jul 26, 2016 at 12:38
  • curiouser and curiouser. if you add the resulting HTML input tag to your question it could help further.
    – majick
    Commented Jul 26, 2016 at 15:30

1 Answer 1

0

Okay I think I have it, you need to add a matching filter to attachment_fields_to_save to save the new field (say as post meta to the attachment.)

add_filter( 'attachment_fields_to_save', 'save_some_custom_field', 10, 2 );
function save_some_custom_field($post, $attachment) {
    $attachid = $post['ID']; // yes this is actually an array here
    update_post_meta($attachid,'someCustom',$attachment['someCustom']);
}

Then you would add to your $form_fields edit filter array something like:

'value' => get_post_meta($post->ID,'someCustom',true),

...remembering to remove the = null from the function arguments.

unset($form_fields['post_excerpt']); and unset($form_fields['post_content']); in the edit filter should remove caption and description fields respectively, not sure on the required part though.

1
  • You are correct. But I've looked in to this a bit more. When you click on an embedded image inside the posts content, and then click the little pen to "edit" the image - a very different modal shows up. You can edit the caption, and the alt text - but nothing else. The template shown on that modal is static, and cannot handle custom fields (see /wp-includes/media-template.php). What we can to is add custom fields to the attachment-post, and they are editable under the Media-menu. In other words, there is no way to have custom fields for images, unique for the blog post. Commented Jul 27, 2016 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.