How to add custom fields to Attachments – 1

First of all, we are going to create a plugin to handle the attachments custom fields. It will get a set of options, bake them so they become part of the form when we edit an attachment, and save them into the database.

For this, we will use two WordPress hooks:

  • attachment_fields_to_edit to handle the edit form
  • attachment_fields_to_save to save the custom fields
  •  

First Step — Creating the Plugin :

Create a new folder in the plugins directory (wp-content/plugins/media-fields/ for example) and put a file (named plugin.php) inside. Let’s also put a file called custom_media_fields.php which will hold our options.

This is what your plugin.php file should look like at first:

[php]/*

Plugin Name: Plugin Name

Plugin URI:

Description: Create attachments custom fields

Version: 0.1

Author:Author Name

Author URI: http://

License: GPL2

*/

require_once( plugin_dir_path( __FILE__ ) . ‘/custom_media_fields.php’ );

Class Wptuts_Custom_Media_Fields {

private $media_fields = array();

function __construct( $fields ) {

}

public function applyFilter( $form_fields, $post = null ) {

}

function saveFields( $post, $attachment ) {

}

}

$cmf = new Wptuts_Custom_Media_Fields( $attchments_options );

[/php]

This is the base we’ll populate in the following posts. For now, let’s define our set of options.

Leave a Reply

Your email address will not be published. Required fields are marked *