Advertisement
mrwweb

Broken Image Author Meta Plugin

Nov 18th, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MarkMediaMeta
  4. Description: A custom plugin for mrwweb.com to add media rights information. Forever indebted to http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/.
  5. Version: 0.5
  6. Author: Mark Root-Wiley
  7. Author URI: http://mrwweb.com
  8. */
  9.  
  10. /**
  11.  * Add Author Name, Author URL, License Text, and License URL to media library.
  12.  *
  13.  * @param $form_fields array, fields to include in attachment form
  14.  * @param $post object, attachment record in database
  15.  * @return $form_fields, modified form fields
  16.  */
  17.  
  18. add_filter( 'attachment_fields_to_edit', 'mrw_media_fields', 10, 2 );
  19.  
  20. function mrw_media_fields( $form_fields, $post ) {
  21.  
  22.     $form_fields['mrw-creator-name'] = array(
  23.         'label' => 'Creator Name',
  24.         'input' => 'text',
  25.         'value' => get_post_meta( $post->ID, 'mrw_creator_name', true ),
  26.         'helps' => 'Name of the content creator.'
  27.     );
  28.    
  29.     $form_fields['mrw-creator-url'] = array(
  30.         'label' => 'Creator URL',
  31.         'input' => 'text',
  32.         'value' => get_post_meta( $post->ID, 'mrw_creator_url', true ),
  33.         'helps' => 'Link to the content creator.'
  34.     );
  35.    
  36.     $form_fields['mrw-license-name'] = array(
  37.         'label' => 'License Name',
  38.         'input' => 'text',
  39.         'value' => get_post_meta( $post->ID, 'mrw_license_name', true ),
  40.         'helps' => 'Media copyright license.'
  41.     );
  42.    
  43.     $form_fields['mrw-license-url'] = array(
  44.         'label' => 'License URL',
  45.         'input' => 'text',
  46.         'value' => get_post_meta( $post->ID, 'mrw_license_url', true ),
  47.         'helps' => 'URL for the media copyright license.'
  48.     );
  49.    
  50.     return $form_fields;
  51. }
  52.  
  53.  
  54. /**
  55.  * Save values to Author Name, Author URL, License Text, and License URL to media library.
  56.  *
  57.  * @param $post array, the post data for database
  58.  * @param $attachment array, attachment fields from $_POST form
  59.  * @return $post array, modified post data
  60.  */
  61.  
  62. add_filter( 'attachment_fields_to_save', 'mrw_media_fields_save', 10, 2 );
  63.  
  64. function mrw_media_fields_save( $post, $attachment ) {
  65.  
  66.     if( isset( $attachment['mrw-creator-name'] ) )
  67.         update_post_meta( $post['ID'], 'mrw_creator_name', $attachment['mrw-creator-name'] );
  68.        
  69.     if( isset( $attachment['mrw-creator-url'] ) )
  70.         update_post_meta( $post['ID'], 'mrw_creator_url', $attachment['mrw-creator-url'] );
  71.    
  72.     if( isset( $attachment['mrw-license-name'] ) )
  73.         update_post_meta( $post['ID'], 'mrw_license_name', $attachment['mrw-license-name'] );
  74.        
  75.     if( isset( $attachment['mrw-license-url'] ) )
  76.         update_post_meta( $post['ID'], 'mrw_license_url', $attachment['mrw-license-url'] );
  77.    
  78.     return $post;
  79. }
  80.  
  81.  
  82. /**
  83.  * Filter to get caption and append image information to it.
  84.  *
  85.  * @return text HTML content describing with media attribution
  86.  **/
  87.  
  88. add_filter('get_image_tag', 'mrw_append_media_credit', 20, 2);
  89.  
  90. function mrw_append_media_credit( $html, $id ) {
  91.    
  92.     $mrw_media_string = mrw_media_credit_string( $id );
  93.        
  94.     $html = $html . $mrw_media_string;
  95.    
  96.     return $html;
  97.    
  98. }
  99.  
  100. /**
  101.  * Append creator stuff to attachment pages.
  102.  **/
  103.  
  104. add_filter('the_content', 'mrw_append_attachment_credit', 20);
  105.  
  106. function mrw_append_attachment_credit( $content ) {
  107.  
  108.     if ( is_attachment() ) {
  109.  
  110.         global $post;
  111.        
  112.         $mrw_media_string = mrw_media_credit_string( $post->ID );
  113.        
  114.         $content = $content . $mrw_media_string;
  115.    
  116.     }
  117.    
  118.     return $content;
  119.    
  120. }
  121.  
  122. /**
  123.  * Function to lookup media creator fields and put them together.
  124.  *
  125.  * returns an html string with creator name and license name links.
  126.  **/
  127.  
  128. function mrw_media_credit_string( $mrw_media_id ) {
  129.  
  130.     $mrw_name = get_post_meta( $mrw_media_id, 'mrw_creator_name', true );
  131.     $mrw_name_url = get_post_meta( $mrw_media_id, 'mrw_creator_url', true );
  132.     $mrw_license_name = get_post_meta( $mrw_media_id, 'mrw_license_name', true );
  133.     $mrw_license_url = get_post_meta( $mrw_media_id, 'mrw_license_url', true );
  134.    
  135.     $mrw_creator = null;
  136.     if( $mrw_name ) {
  137.         if( $mrw_name_url ) {
  138.             $mrw_creator = ' <a href="' . $mrw_name_url . '">' . $mrw_name . '</a>.';  
  139.         } else {
  140.             $mrw_creator = ' ' . $mrw_name;
  141.         }
  142.     }
  143.    
  144.     $mrw_license = null;
  145.     if( $mrw_license_name ) {
  146.         if( $mrw_license_url ) {
  147.             $mrw_license = ' <a href="' . $mrw_license_url . '">' . $mrw_license_name . '</a>.';   
  148.         } else {
  149.             $mrw_license = ' ' . $mrw_license_name;
  150.         }
  151.     }
  152.    
  153.     return '<span class="mrw-media-attr">Credit:' . $mrw_creator . $mrw_license . '</span>';
  154.  
  155. }
  156.  
  157. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement