|
I use a
very rough
plugin to add information about the artist and a URL to media files. It needs some tweaking (and I need the time), but it works and may demonstrate how add the extra fields and how to use them in your theme:
array ( 'public' =>
'artist_name' , 'hidden' =>
'_artist_name' , 'label' =>
'Fotograf (Name)' ) , 'artist_url' =>
array ( 'public' =>
'artist_url' , 'hidden' =>
'_artist_url' , 'label' =>
'Fotograf (URL)' ) )
, 'Foto: '
);
/** * Adds two fields for credits to any media file: name and URL. * * Based on the clear tutorial by Andy Blackwell: * @link http://net.tutsplus.com/?p=13076 */
class Media_Artist
{ public $fields = array ( 'artist_name' =>
array ( 'public' =>
'artist_name' , 'hidden' =>
'_artist_name' , 'label' =>
'Artist Name' ) , 'artist_url' =>
array ( 'public' =>
'artist_url' , 'hidden' =>
'_artist_url' , 'label' =>
'Artist URL' ) ) // Maybe its own field? , $caption_prefix , $br_before = TRUE; public function __construct( $fields = array() , $caption_prefix = 'Source: ' , $br_before = TRUE ) { $this->
fields = array_merge($this->
fields, $fields); $this->
caption_prefix = $caption_prefix; $this->
br_before = (bool) $br_before; $this->
set_filter(); } public function set_filter() { add_filter( 'attachment_fields_to_edit' , array ( $this, 'add_fields' ) , 15 , 2 ); add_filter( 'attachment_fields_to_save' , array ( $this, 'save_fields' ) , 10 , 2 ); add_filter( 'img_caption_shortcode' , array ( $this, 'caption_filter' ) , 1 , 3 ); } public function add_fields($form_fields, $post) { foreach ( $this->
fields as $field) { $form_fields[ $field['public'] ]['label'] = $field['label']; $form_fields[ $field['public'] ]['input'] = ext'; $form_fields[ $field['public'] ]['value'] = get_post_meta( $post->
ID , $field['hidden'] , TRUE ); } return $form_fields; } public function save_fields($post, $attachment) { foreach ( $this->
fields as $field) { if ( isset ( $attachment[ $field['public'] ]) ) { update_post_meta( $post['ID'] , $field['hidden'] , $attachment[ $field['public'] ] ); } } return $post; } public function caption_filter($empty, $attr, $content = '') { /* Typical input: * [caption id="attachment_525" align="aligncenter" * width="300" caption="The caption."] *
[/caption] */ extract( shortcode_atts( array ( 'id' =>
'' , 'align' =>
'alignnone' , 'width' =>
'' , 'caption' =>
'' , 'nocredits' =>
'0' ) , $attr ) ); // Let WP handle these cases. if ( empty ($id ) or 1 == $nocredits ) { return ''; } if ( 1 >
(int) $width || empty ( $caption ) ) { return $content; } if ( ! empty ( $id ) ) { // Example: attachment_525 $html_id = 'id="' . esc_attr($id) . '" '; $tmp = explode('_', $id); $id = end($tmp); $sub_caption = ''; $artist_name = get_post_meta($id, $this->
fields['artist_name']['hidden'], TRUE); $artist_url = get_post_meta($id, $this->
fields['artist_url']['hidden'], TRUE); // Okay, at least one value. if ( '' != $artist_name . $artist_url ) { $sub_caption .= $this->
br_before ? '
' : ''; $sub_caption .= '
' . $this->
caption_prefix; // No name given. We use the shortened URL. if ( '' == $artist_name ) { $sub_caption .= '
' . $this->
short_url($artist_url) . '
'; } // We have just the name. elseif ( '' == $artist_url ) { $sub_caption .= $artist_name; } // We have both. else { $sub_caption .= '
' . $artist_name . '
'; } $sub_caption .= '
'; } $caption .= $sub_caption; } return '
' . do_shortcode( $content ) . '
' . $caption . '
'; } public function short_url($url, $max_length=20) { $real_length = mb_strlen($url, 'UTF-8'); if ( $real_length <= $max_length ) { return $url; } $keep = round( $max_length / 2 ) - 1; return mb_substr($url, 0, $keep, 'UTF-8') . '…' . mb_substr($url, -$keep, $real_length, 'UTF-8'); } # @todo uninstall
}
|