This is a WordPress plugin. [Official download available on WordPress Extend](http://wordpress.org/extend/plugins/attachments/). # Attachments Attachments allows you to simply append any number of items from your WordPress Media Library to Posts, Pages, and Custom Post Types * [Description](#description) * [Installation](#installation) * **[Upgrade Notice](#upgrade-notice)** *Pay specific attention if upgrading from a version before 3.0* * [Usage](#usage) * [Disable Settings Screen](#disable-settings-screen) * [Setting Up Instances](#setting-up-instances) - Create meta boxes for your Posts, Pages, and Custom Post Types * [Disable the Default Instance](#disable-the-default-instance) * [Create Custom Instances](#create-custom-instances) * [Fields Reference](#fields-reference) * [Pulling Attachments to your Theme](#pulling-attachments-to-your-theme) * [Retrieve Attachments Outside The Loop](#retrieve-attachments-outside-the-loop) * [Retrieve Attachment Attributes](#retrieve-attachment-attributes) * [Retrieve Single Attachments](#retrieve-single-attachments) * [Search](#search) * [Filters](#filters) * [Post Meta Key](#post-meta-key) - Change the `meta_key` used to store Attachments' data * [Get Attachments](#get-attachments) - Edit the order of Attachments in your theme * [Screenshots](#screenshots) * [Frequently Asked Questions](#frequently-asked-questions) * [Changelog](#changelog) * [Roadmap](#roadmap) ## Description Attachments allows you to simply append any number of items from your WordPress Media Library to Posts, Pages, and Custom Post Types. This plugin *does not* directly interact with your theme, you will need to edit your template files. ### Updated for WordPress 3.5! WordPress 3.5 ships with an amazing new Media workflow and Attachments 3.0 makes great use of it. *If you are not running WordPress 3.5, the (now deprecated) version 1.6.2.1 (included with Attachments 3.x) will be used until you upgrade to WordPress 3.5+* ### Associate Media items with posts The idea behind Attachments is to give developers the ability to directly associate Media items with any post. This is accomplished by adding a meta box to post edit screens as determined by the developer. Once Media items have been associated with a post, you're able to retrieve those Attachments and include them directly within your template files using any specific markup you wish. ### Integrate Attachments within your theme with fine grained control **Attachments does not automatically integrate itself with your theme.** Since the idea behind Attachments is to allow integration of Media within posts using developer-crafted, unique markup, *it's up to you to integrate with your theme*. The most basic integration includes editing the [appropriate template file](http://codex.wordpress.org/Template_Hierarchy) and adding your call(s) to Attachments. For example, if you have set up Attachments to be used with your Posts entries, edit `single.php` to include the following within The Loop: ```php exist() ) : ?>

Attachments

``` That snippet will request all of the existing Attachments defined for the current Post within The Loop, and retrieve each itemized property for that Attachment. Using the provided details you're able to integrate the attached Media items in any way you please. ## Installation 1. Download the plugin and extract the files 1. Upload `attachments` to your `~/wp-content/plugins/` directory 1. Activate the plugin through the 'Plugins' menu in WordPress 1. Implement Attachments in your theme's `functions.php` or your own plugin (see **[Usage](#usage)**) 1. Update your templates where applicable (see **[Usage](#usage)**) ## Upgrade Notice #### 3.0 **You will need to update your theme files that use Attachments 3.0**. Version 1.x of Attachments has been **fully deprecated** but is still available *and included with Attachments 3.x*. If you would like to continue to use the (no longer supported) 1.x version you may add the following to your `wp-config.php`: ```php define( 'ATTACHMENTS_LEGACY', true ); // force the legacy version of Attachments ``` Version 3 is a **major** rewrite. While I've taken precautions in ensuring you won't lose any saved data it is important to back up your databse prior to upgrading in case something goes wrong. This version is a complete rewrite so all legacy data will be left in place, but a migration must take place to match the new data storage model and workflow. ## Usage Attachments is based on *instances* which correlate directly with the meta boxes that appear on edit screens of Posts, Pages, and Custom Post Types. By default Attachments ships with a single meta box that appears *only on Posts and Pages*. It has two fields: one for Title and one for Caption. If you would like to disable or customize the default instance, or you'd like to create additional instances with custom fields for different post types, please see [Setting Up Instances](#setting-up-instances). ### Disable Settings Screen Attachments ships with a `Settings` screen (found under the `Settings` menu in the main WordPress admin navigation) that facilitates data migration from version 1.x and also offers some code snippets. If you would like to **disable the Settings screen** add the following to your theme's `functions.php`: ```php define( 'ATTACHMENTS_SETTINGS_SCREEN', false ); // disable the Settings screen ``` ### Setting Up Instances When Attachments is first activated, a default instance is created titled Attachments. It has two fields: 1. Title 1. Caption #### Disable the Default Instance If you would like to *disable the default instance* (meta box titled 'Attachments' with a 'Title' and 'Caption' field) add the following to your `wp-config.php` *before* `require_once(ABSPATH . 'wp-settings.php');`: ```php define( 'ATTACHMENTS_DEFAULT_INSTANCE', false ); ``` #### Create Custom Instances You may create instances with your own custom fields by using the `attachments_register` action. To create your own instance add the following to your theme's `functions.php` or your own plugin: ```php 'title', // unique field name 'type' => 'text', // registered field type 'label' => __( 'Title', 'attachments' ), // label to display 'default' => 'title', // default value upon selection ), array( 'name' => 'caption', // unique field name 'type' => 'textarea', // registered field type 'label' => __( 'Caption', 'attachments' ), // label to display 'default' => 'caption', // default value upon selection ), ); $args = array( // title of the meta box (string) 'label' => 'My Attachments', // all post types to utilize (string|array) 'post_type' => array( 'post', 'page' ), // meta box position (string) (normal, side or advanced) 'position' => 'normal', // meta box priority (string) (high, default, low, core) 'priority' => 'high', // allowed file type(s) (array) (image|video|text|audio|application) 'filetype' => null, // no filetype limit // include a note within the meta box (string) 'note' => 'Attach files here!', // by default new Attachments will be appended to the list // but you can have then prepend if you set this to false 'append' => true, // text for 'Attach' button in meta box (string) 'button_text' => __( 'Attach Files', 'attachments' ), // text for modal 'Attach' button (string) 'modal_text' => __( 'Attach', 'attachments' ), // which tab should be the default in the modal (string) (browse|upload) 'router' => 'browse', // fields array 'fields' => $fields, ); $attachments->register( 'my_attachments', $args ); // unique instance name } add_action( 'attachments_register', 'my_attachments' ); ``` #### Fields Reference At this time there are **four** field types available: 1. `text` 1. `textarea` 1. `select` 1. `wysiwyg` When declaring fields for your instance, you'll be composing an array of fields, each with an array of parameters that set the various attributes of each field. Here is a full example of all available parameters for all available fields: ```php /** * Fields for the instance are stored in an array. Each field consists of * an array with three required keys: name, type, label * and one optional key: meta * * name - (string) The field name used. No special characters. * type - (string) The registered field type. * Fields available: text, textarea, wysiwyg, select * label - (string) The label displayed for the field. * default - (string) The default WordPress metadata to use when initially adding the Attachment * Defaults available: title, caption, alt, description * meta - (array) The field-specific parameters that apply only to that field type */ $fields => array( array( 'name' => 'title', // unique field name 'type' => 'text', // registered field type 'label' => __( 'Title', 'attachments' ), // label to display 'default' => 'title', // default value upon selection ), array( 'name' => 'caption', // unique field name 'type' => 'textarea', // registered field type 'label' => __( 'Caption', 'attachments' ), // label to display 'default' => 'caption', // default value upon selection ), array( 'name' => 'option', // unique field name 'type' => 'select', // registered field type 'label' => __( 'Option', 'attachments' ), // label to display 'meta' => array( // field-specific meta as defined by field class 'allow_null' => true, // allow null value? (adds 'empty'