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
此函数可以在插件或主题中的任何位置使用。我在single上使用了该功能。像这样的php
<?php if( function_exists(\'base_image_credit\') ) echo base_image_credit(); ?>
原始的
tutorial当做