Add field to media edit form

24th Jun 2011 at 10:14 AM | Posted in Wordpress | 3 Comments
<?php
// hook media edit form
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); function my_image_attachment_fields_to_edit($form_fields, $post) { 	// $form_fields is a special array of fields to include in the attachment form 	// $post is the attachment record in the database 	//     $post--->post_type == 'attachment'
	// (attachments are treated as posts in WordPress)

	// add our custom field to the $form_fields array
	// input type="text" name/id="attachments[$attachment->ID][custom1]"
	$checked = get_post_meta($post->ID, "_is_photo_gallery", true);
	if($checked){
		$is_checked="checked";
	}else{
		$is_checked='';
	}
	$form_fields["is_photo_gallery"] = array(
		"label" => __("Show In Photo Gallery"),
		"input" => "html", // this is default if "input" is omitted
		"html" => "<input id="attachments[{$post->ID}][is_photo_gallery]" type="checkbox" name="attachments[{$post->ID}][is_photo_gallery]" value="1" />"
	);
	// if you will be adding error messages for your field,
	// then in order to not overwrite them, as they are pre-attached
	// to this array, you would need to set the field up like this:
	//$form_fields["custom1"]["label"] = __("Custom Text Field");
	//$form_fields["custom1"]["input"] = "text";
	//$form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);

	return $form_fields;
}
function my_image_attachment_fields_to_save($post, $attachment) {
	// $attachment part of the form $_POST ($_POST[attachments][postID])
	// $post attachments wp post array - will be saved after returned
	//     $post['post_type'] == 'attachment'
	if( isset($attachment['is_photo_gallery']) ){
		// update_post_meta(postID, meta_key, meta_value);
		update_post_meta($post['ID'], '_is_photo_gallery', $attachment['is_photo_gallery']);
	}
	return $post;
}
?>

Output:

Advertisements

3 Comments »

RSS feed for comments on this post. TrackBack URI

  1. You are my inhalation, I have few web logs and rarely run out from post bebdddbbcecd

  2. The data are part of an autumn of just about 16 targets in Lib Dem provide because of the 2010 commander selection, before Huhne scooped 46.5 with all the electionHermes kelly felix bags didn can far in order to move benefit eabegadeacad

  3. I think this is one of the most important info for me. And i am glad reading your article. But wanna remark on few general things, The web site style is ideal, the articles is really excellent D. Good job, cheers dfdaddddebkkeadc


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: