I want to create a "photography" custom post type which uses attachment's same edit/upload panel. Exact same panels usage for "attachment" post type but with "photography" name. Is it possible?
3 Answers
It seems its impossible with WordPress 3.5 at the moment. I opened an idea for it: http://wordpress.org/extend/ideas/topic/custom-attachment-type Support if you like idea please.
Adding Additional Fields
function add_image_attachment_fields_to_edit( $form_fields, $post ) {
// Remove the "Description" field, we're not using it
unset( $form_fields['post_content'] );
// Add description text (helps) to the "Title" field
$form_fields['post_title']['helps'] = 'Use a descriptive title for the image. This will make it easy to find the image in the future and will improve SEO.';
// Re-order the "Caption" field by removing it and re-adding it later
$form_fields['post_excerpt']['helps'] = 'Describe the significants of the image pertaining to the site.';
$caption_field = $form_fields['post_excerpt'];
unset($form_fields['post_excerpt']);
// Re-order the "File URL" field
$image_url_field = $form_fields['image_url'];
unset($form_fields['image_url']);
// Add Caption before Credit field
$form_fields['post_excerpt'] = $caption_field;
// Add a Credit field
$form_fields["credit_text"] = array(
"label" => __("Credit"),
"input" => "text", // this is default if "input" is omitted
"value" => esc_attr( get_post_meta($post->ID, "_credit_text", true) ),
"helps" => __("The owner of the image."),
);
// Add a Credit field
$form_fields["credit_link"] = array(
"label" => __("Credit URL"),
"input" => "text", // this is default if "input" is omitted
"value" => esc_url( get_post_meta($post->ID, "_credit_link", true) ),
"helps" => __("Attribution link to the image source or owners website."),
);
// Add Caption before Credit field
$form_fields['image_url'] = $image_url_field;
return $form_fields;
}
add_filter("attachment_fields_to_edit", "add_image_attachment_fields_to_edit", null, 2);
Save The Data as Custom Fields
function add_image_attachment_fields_to_save( $post, $attachment ) {
if ( isset( $attachment['credit_text'] ) )
update_post_meta( $post['ID'], '_credit_text', esc_attr($attachment['credit_text']) );
if ( isset( $attachment['credit_link'] ) )
update_post_meta( $post['ID'], '_credit_link', esc_url($attachment['credit_link']) );
return $post;
}
add_filter("attachment_fields_to_save", "add_image_attachment_fields_to_save", null , 2);
Display Attachment Custom Fields
function base_image_credit( $post_ID = null ) {
// Get the post ID of the current post if not set
if ( !$post_ID ) {
global $post;
$post_ID = $post->ID;
}
// Get all the attachments for the current post (object stdClass)
$attachments = get_children('post_type=attachment&post_parent=' . $post->ID);
// If attachments are found
if ( isset($attachments) && !empty($attachments) ) {
// Get the first attachment
$first_attachment = current($attachments);
$attachment_fields = get_post_custom( $first_attachment->ID );
// Get custom attachment fields
$credit_text = ( isset($attachment_fields['_credit_text'][0]) && !empty($attachment_fields['_credit_text'][0]) ) ? esc_attr($attachment_fields['_credit_text'][0]) : '';
$credit_link = ( isset($attachment_fields['_credit_link'][0]) && !empty($attachment_fields['_credit_link'][0]) ) ? esc_url($attachment_fields['_credit_link'][0]) : '';
// Output HTML if you want
$credit = ( $credit_text && $credit_link ) ? "Image provided by <a href='$credit_link'>$credit_text</a>" : false;
}
return $credit;
}
Using the display image credit function
This function can be used anywhere in a plugin or theme. I’ve used the functionality on single.php like this
<?php if( function_exists('base_image_credit') ) echo base_image_credit(); ?>
Orginal tutorial
Regards
-
1how is adding custom fields make an attachment post type as "photography" custom post type? Commented Jan 13, 2013 at 17:51
You can still add the codes in your theme’s functions.php file to make it work.
function be_attachment_field_credit( $form_fields, $post ) {
$form_fields['be-photographer-name'] = array(
'label' => 'Photographer Name',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
'helps' => 'If provided, photo credit will be displayed',
);
$form_fields['be-photographer-url'] = array(
'label' => 'Photographer URL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
'helps' => 'Add Photographer URL',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );
/**
* Save values of Photographer Name and URL in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function be_attachment_field_credit_save( $post, $attachment ) {
if( isset( $attachment['be-photographer-name'] ) )
update_post_meta( $post['ID'], 'be_photographer_name', $attachment['be-photographer-name'] );
if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
?>
If you want to display the fields in your attachments template, then simply paste the following codes inside the loop:
echo get_post_meta($post->ID, 'be_photographer_url', true);
If you want to display the fields for your featured image in your archive template or any other template, then simply use:
echo get_post_meta(get_post_thumbnail_id(), 'be_photographer_url', true);