Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: MarkMediaMeta
- 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/.
- Version: 0.5
- Author: Mark Root-Wiley
- Author URI: http://mrwweb.com
- */
- /**
- * Add Author Name, Author URL, License Text, and License URL to media library.
- *
- * @param $form_fields array, fields to include in attachment form
- * @param $post object, attachment record in database
- * @return $form_fields, modified form fields
- */
- add_filter( 'attachment_fields_to_edit', 'mrw_media_fields', 10, 2 );
- function mrw_media_fields( $form_fields, $post ) {
- $form_fields['mrw-creator-name'] = array(
- 'label' => 'Creator Name',
- 'input' => 'text',
- 'value' => get_post_meta( $post->ID, 'mrw_creator_name', true ),
- 'helps' => 'Name of the content creator.'
- );
- $form_fields['mrw-creator-url'] = array(
- 'label' => 'Creator URL',
- 'input' => 'text',
- 'value' => get_post_meta( $post->ID, 'mrw_creator_url', true ),
- 'helps' => 'Link to the content creator.'
- );
- $form_fields['mrw-license-name'] = array(
- 'label' => 'License Name',
- 'input' => 'text',
- 'value' => get_post_meta( $post->ID, 'mrw_license_name', true ),
- 'helps' => 'Media copyright license.'
- );
- $form_fields['mrw-license-url'] = array(
- 'label' => 'License URL',
- 'input' => 'text',
- 'value' => get_post_meta( $post->ID, 'mrw_license_url', true ),
- 'helps' => 'URL for the media copyright license.'
- );
- return $form_fields;
- }
- /**
- * Save values to Author Name, Author URL, License Text, and License URL to media library.
- *
- * @param $post array, the post data for database
- * @param $attachment array, attachment fields from $_POST form
- * @return $post array, modified post data
- */
- add_filter( 'attachment_fields_to_save', 'mrw_media_fields_save', 10, 2 );
- function mrw_media_fields_save( $post, $attachment ) {
- if( isset( $attachment['mrw-creator-name'] ) )
- update_post_meta( $post['ID'], 'mrw_creator_name', $attachment['mrw-creator-name'] );
- if( isset( $attachment['mrw-creator-url'] ) )
- update_post_meta( $post['ID'], 'mrw_creator_url', $attachment['mrw-creator-url'] );
- if( isset( $attachment['mrw-license-name'] ) )
- update_post_meta( $post['ID'], 'mrw_license_name', $attachment['mrw-license-name'] );
- if( isset( $attachment['mrw-license-url'] ) )
- update_post_meta( $post['ID'], 'mrw_license_url', $attachment['mrw-license-url'] );
- return $post;
- }
- /**
- * Filter to get caption and append image information to it.
- *
- * @return text HTML content describing with media attribution
- **/
- add_filter('get_image_tag', 'mrw_append_media_credit', 20, 2);
- function mrw_append_media_credit( $html, $id ) {
- $mrw_media_string = mrw_media_credit_string( $id );
- $html = $html . $mrw_media_string;
- return $html;
- }
- /**
- * Append creator stuff to attachment pages.
- **/
- add_filter('the_content', 'mrw_append_attachment_credit', 20);
- function mrw_append_attachment_credit( $content ) {
- if ( is_attachment() ) {
- global $post;
- $mrw_media_string = mrw_media_credit_string( $post->ID );
- $content = $content . $mrw_media_string;
- }
- return $content;
- }
- /**
- * Function to lookup media creator fields and put them together.
- *
- * returns an html string with creator name and license name links.
- **/
- function mrw_media_credit_string( $mrw_media_id ) {
- $mrw_name = get_post_meta( $mrw_media_id, 'mrw_creator_name', true );
- $mrw_name_url = get_post_meta( $mrw_media_id, 'mrw_creator_url', true );
- $mrw_license_name = get_post_meta( $mrw_media_id, 'mrw_license_name', true );
- $mrw_license_url = get_post_meta( $mrw_media_id, 'mrw_license_url', true );
- $mrw_creator = null;
- if( $mrw_name ) {
- if( $mrw_name_url ) {
- $mrw_creator = ' <a href="' . $mrw_name_url . '">' . $mrw_name . '</a>.';
- } else {
- $mrw_creator = ' ' . $mrw_name;
- }
- }
- $mrw_license = null;
- if( $mrw_license_name ) {
- if( $mrw_license_url ) {
- $mrw_license = ' <a href="' . $mrw_license_url . '">' . $mrw_license_name . '</a>.';
- } else {
- $mrw_license = ' ' . $mrw_license_name;
- }
- }
- return '<span class="mrw-media-attr">Credit:' . $mrw_creator . $mrw_license . '</span>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement