Make WordPress Core

Ticket #32965: attachment-credit_v0-2.php

File attachment-credit_v0-2.php, 3.0 KB (added by mcuomo2013, 10 years ago)

Small plugin that adds Credit field to attachment details. Needs some work.

Line 
1<?php
2/*
3Plugin Name: Attachment Credit Field
4Description: Adds a credit field to the attachment details pane to add attribution information.
5Version:     0.2
6Author:      Michael V Cuomo
7Author URI:  http://www.michaelvcuomo.com
8License:     GPL2
9License URI: https://www.gnu.org/licenses/gpl-2.0.html
10*/
11
12/* Start adding functions below this line */
13
14/*
15 * Notes:
16 *
17 * This plugin was developed as a test to see how to add a credit field to the attachment page. While it
18 * does work, there are some differences between this field and the "standard" fields. Namely, this returns
19 * the credit field as part of a table, while the others are being returned as a label with CSS classes. This
20 * therefore is affecting CSS styles in some areas, like the attachment editor page. Also, looking through
21 * the media.php file shows there is a lot more going on with current attachment details, like returning shortcodes
22 * for the caption--img_caption_shortcode()-- and such. I am not that experienced with working with WordPress code
23 * so I'm not sure how much I can help. Anyone with more experience might be better, or this might want to be included
24 * in the current code, like in the array in get_attachment_fields_to_edit().
25 *
26 * I haven't yet been able to figure out how to display this in my theme. I currently am using the caption field for
27 * image attribution for Featured Images and display it using:
28 *      echo '<cite class="feat-image-caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</cite>';
29 * It is not working by adding post-credit, though. Just keep that in mind -- I'll still mess around and update
30 * if I find a solution.
31 *
32 */
33
34function mvc_add_attachment_credit_field( $form_fields, $post ) {
35    // See wp-admin/includes/media.php function get_attachment_fields_to_edit() line 1164
36    //
37    // Add new form field to attachment details page called Credit.
38    // Credit field allows users to add attribution information to media files.
39   
40    $field_value = get_post_meta( $post->ID, 'post-credit', true );
41   
42    $form_fields['post-credit'] = array(
43        'label' => __( 'Credit' ),
44        'input' => 'textarea',
45        'value' => $field_value ? $field_value : '',
46        //'helps' => __( 'Information to credit the source of this uploaded file.' )
47    );
48   
49    return $form_fields;
50}
51
52// Attach function to hook
53add_filter( 'attachment_fields_to_edit', 'mvc_add_attachment_credit_field', 10, 2 );
54
55// Save Credit field
56function mvc_save_attachment_credit( $attachment_id ) {
57   
58    if ( isset( $_REQUEST['attachments'][$attachment_id]['post-credit'] ) ) {
59       
60        $credit = $_REQUEST['attachments'][$attachment_id]['post-credit'];
61       
62        update_post_meta( $attachment_id, 'post-credit', $credit );
63    }
64}
65// Attach function to hook -- THIS ONE DOES WORK
66add_action( 'edit_attachment', 'mvc_save_attachment_credit' );
67
68// Original - Attach function to hook -- THIS ONE NOT WORKING FOR ME - won't save info
69//add_filter ( 'attachment_fields_to_save','mvc_save_attachment_credit' );
70
71/* Stop adding functions below this line */
72
73?>