3

is it possible to add extra fields via the functions.php script for attachments in wordpress?

Tried loads of examples but none seem to work. Worried an existing plugin might be affecting my attempts but its not clear if its even possible.

best, dan.

4 Answers 4

3

Here is a tutorial that shows how to add custom fields to the attachments/media gallery/thickbox/iframe/whatever-you-call it overlay.

I've successfully used it, but have not yet taken it much further by adding radio buttons/checkboxes/etc or messed with limiting the changes to particular post types, but that all looks completely doable too.

Here is the code from the link above, just in case it disappears some day:

1) 'attachment_fields_to_edit' : We will attach a function to this hook which will do the job of adding a custom field to Media Gallery.

/* For adding custom field to gallery popup */
function rt_image_attachment_fields_to_edit($form_fields, $post) {
    // $form_fields is a an array of fields to include in the attachment form
    // $post is nothing but attachment record in the database
    //     $post->post_type == 'attachment'
    // attachments are considered as posts in WordPress. So value of post_type in wp_posts table will be attachment
    // now add our custom field to the $form_fields array
    // input type="text" name/id="attachments[$attachment->ID][custom1]"
    $form_fields["rt-image-link"] = array(
        "label" => __("Custom Link"),
        "input" => "text", // this is default if "input" is omitted
        "value" => get_post_meta($post->ID, "_rt-image-link", true),
                "helps" => __("To be used with special slider added via [rt_carousel] shortcode."),
    );
   return $form_fields;
}

2) 'attachment_fields_to_save' : This in turn will accept and save the user input.

// now attach our function to the hook
add_filter("attachment_fields_to_edit", "rt_image_attachment_fields_to_edit", null, 2);

    function rt_image_attachment_fields_to_save($post, $attachment) {
    // $attachment part of the form $_POST ($_POST[attachments][postID])
        // $post['post_type'] == 'attachment'
    if( isset($attachment['rt-image-link']) ){
        // update_post_meta(postID, meta_key, meta_value);
        update_post_meta($post['ID'], '_rt-image-link', $attachment['rt-image-link']);
    }
    return $post;
}
// now attach our function to the hook.
add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
1
  • @daniel-crabbe You're welcome! Just FYI, it looks like the code linked in toscho's answer takes it to the next level, if you feel like going there ;) Commented Apr 12, 2011 at 17:14
2

Here is a very good tutorial (with source files), explains how to add custom fields for images, attachments, textareas...and everything else you may need for your posts.

http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html

3
  • 1
    tried this before but as far as i can tell doesn't allow you to add fields to an attachment edit page...
    – v3nt
    Commented Apr 12, 2011 at 14:45
  • ooops, sorry for the inconvenience @daniel Crabbe
    – Philip
    Commented Apr 12, 2011 at 20:29
  • The latest version of Meta Box, the version made from this tutorial, supports custom fields for images.
    – Anh Tran
    Commented Dec 12, 2015 at 16:50
1

You need to use the update_post_meta() (Codex) function:

<?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?>

What are you trying to do, that isn't working?

4
  • just add extra fields/meta boxes as default for the media/attachment page. ie year, mediaType... Tried severl variations of code that work for posts & pages but not the attachments.
    – v3nt
    Commented Apr 11, 2011 at 17:53
  • Attachments don't have an "Edit" page in the Admin UI. You could possibly hook into the Media Manger to add this custom meta data, though? Commented Apr 11, 2011 at 17:57
  • But what I was more asking was: what methods have you tried, that aren't working? Commented Apr 11, 2011 at 18:00
  • several including some plugins but think the uninstalled next gen gallery is causing probs... goto10's code seems to be working though!
    – v3nt
    Commented Apr 12, 2011 at 14:45
1

You will find good sample code in the answers to question: How can I add a URL field to the attachments window?

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.