Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

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);
share|improve this answer
    
thanks goto10! This seems to work... –  daniel Crabbe Apr 12 '11 at 14:46
    
@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 ;) –  goto10 Apr 12 '11 at 17:14
add comment

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

share|improve this answer
    
tried this before but as far as i can tell doesn't allow you to add fields to an attachment edit page... –  daniel Crabbe Apr 12 '11 at 14:45
    
ooops, sorry for the inconvenience @daniel Crabbe –  Philip Apr 12 '11 at 20:29
add comment

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?

share|improve this answer
    
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. –  daniel Crabbe Apr 11 '11 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? –  Chip Bennett Apr 11 '11 at 17:57
    
But what I was more asking was: what methods have you tried, that aren't working? –  Chip Bennett Apr 11 '11 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! –  daniel Crabbe Apr 12 '11 at 14:45
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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