emerico
5/6/2016 - 11:24 AM

Add custom field to attachments in wordpress

Add custom field to attachments in wordpress

<?php
/**
 *
 * @wordpress-plugin
 * Plugin Name:       Attachment custom fields
 * Plugin URI:        http://uditrawat.com
 * Description:       Custom fields for Attachments
 * Version:           10.3
 * Author:            Udit Rawat
 * Author URI:        http://uditrawat.com
 * Text Domain:       emerico
 */

class AttachmentCustomFields {

    /**
     * Instance of this class.
     *
     * @since    1.0.0
     *
     * @var      object
     */
    protected static $instance = null;

    /**
     * Initialize the plugin by setting localization and loading public scripts
     * and styles.
     *
     * @since     1.0.0
     */
    private function __construct() {

        // Adding custom field
        add_filter("attachment_fields_to_edit", [$this,"add_attachment_custom_fields"], null, 2);

        // Save custom field
        add_filter("attachment_fields_to_save", [$this,"save_attachment_custom_fields"], null, 2);

    }

    /**
     * Return an instance of this class.
     *
     * @since     1.0.0
     *
     * @return    object    A single instance of this class.
     */
    public static function get_instance() {

        // If the single instance hasn't been set, set it now.
        if ( null == self::$instance ) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * @param $form_fields
     * @param $post
     * @return mixed
     * Add custom field to attachment
     */
    function add_attachment_custom_fields($form_fields, $post){

        $form_fields["custom_field_one"] = [
            "label" => __("Custom Text Field"),
            "input" => "text", // this is default if "input" is omitted
            "value" => get_post_meta($post->ID, "custom_field_one", true)
        ];

        return $form_fields;

    }

    /**
     * @param $post
     * @param $attachment
     * @return mixed
     * Save value of custom field values
     */
    function save_attachment_custom_fields($post, $attachment){
        if( isset($attachment['custom_field_one']) ){
            // update_post_meta(postID, meta_key, meta_value);
            update_post_meta($post['ID'], 'custom_field_one', $attachment['custom_field_one']);
        }
        return $post;
    }

}

add_action( 'plugins_loaded', array( 'AttachmentCustomFields', 'get_instance' ) );
/*

YOU CAN ADD OTHER TYPE OF FIELD AS

1:- TEXT AREA
$form_fields["custom_text_area"] = [
            "label" => __("Custom Text Area"),
            "input" => "textarea", // this is default if "input" is omitted
            "value" => get_post_meta($post->ID, "custom_text_area", true)
        ];

2:- HIDDEN FIELD
$form_fields["custom_hidden"] = [
            "input" => "hidden",
            "value" => get_post_meta($post->ID, "custom_hidden", true)
        ];

3:- OTHER FIELDS
$form_fields["custom_select"]["label"] = __("Custom Select");
$form_fields["custom_select"]["input"] = "html";
$form_fields["custom_select"]["html"] = "
<select name='attachments[{$post->ID}][custom_select]' id='attachments[{$post->ID}][custom_select]'>
    <option value='1'>Option 1</option>
    <option value='2'>Option 2</option>
    <option value='3'>Option 3</option>
</select>";

// ANOTHER EXAMPLE
$form_fields["custom_checkbox"]["label"] = __("Custom Checkbox");
$form_fields["custom_checkbox"]["input"] = "html";
$form_fields["custom_checkbox"]["html"] = "the html output goes here, like a checkbox:
<input type='custom_checkbox' value='1'
    name='attachments[{$post->ID}][custom_checkbox]'
    id='attachments[{$post->ID}][custom_checkbox]' />";

4:- SPECIAL ATTRIBUTE
$form_fields["custom_help_text"] = [
        "label" => __("Custom Field with Helpful Text"),
        "value" => get_post_meta($post->ID, "custom_help_text", true),
        "helps" => "Put helpful text here.",
    ];

5:- REQUIRED FIELDS
add required attribute
$form_fields["custom_field_required"] = [
            "label" => __("Required Text Field"),
            "input" => "text",
            "value" => get_post_meta($post->ID, "custom_field_required", true),
            "required" => TRUE
        ];


6:- EXTRA ROWS
$form_fields["custom_tr_row"]["tr"] = "
<tr id='colored_tr'>
    <td colspan='2' style='background:#888;color:#fff;'>
        You can write your fields heading here
    </td>
</tr>";