views:

20

answers:

1

I'm using the WordPress "attachment" feature to allow end users of my theme to upload images that will appear above the post content (not inserted into the post itself).

The only problem I have is that there is not a field to allow the end user to specify the link that should be loaded when the end user clicks on one of the attached images. I'd like to add this field to the post attachment editor (the one that lists the "Gallery" of images attached to the post).

Alternately, and perhaps in addition, I'd like to be able to do the same thing when viewing images via the Media manager listing.

Currently, I'm using the "description" field to store the hyperlink to the image. and retrieving it like so (works perfectly but description is not semantic to link destination):

if ($images = get_children(array('post_parent' => get_the_ID(),'post_type' => 'attachment','post_mime_type' => 'image', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC'    )))
    {
    foreach( $images as $image ) :
        echo "<a href='".$image->post_content."'><img src='".wp_get_attachment_url($image->ID, 'medium')."' /></a>";
    endforeach;
    }   
}
A: 
function my_image_attachment_fields_to_edit($form_fields, $post) {  
    $form_fields["custom1"] = array(  
        "label" => __("Image Links To"),  
        "input" => "text", // this is default if "input" is omitted  
        "value" => get_post_meta($post->ID, "_custom1", true)  
    );    
    return $form_fields;  
}  

function my_image_attachment_fields_to_save($post, $attachment) {  
    if( isset($attachment['custom1']) ){  
        update_post_meta($post['ID'], '_custom1', $attachment['custom1']);  
    }  
    return $post;  
} 

add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2); 
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2); 
Scott B