Skip to content

Attachment

Radoslav Georgiev edited this page Oct 21, 2018 · 4 revisions

Intro

Internally all user-uploaded files in WordPress are called Attachments.

Ultimate FIelds allows you to add additional fields to all attachment types with the fields being visible in the media popup through the Attachment location.

media-fields

Usage through the interface

To add fields to attachments with the the Administration Interface, please follow these steps:

  1. If you are not on the "Add Container" screen already, locate the "Ultimate Fields" section in the administration area and click the "Add New" button on the top (next to the "Containers" title).
  2. Locate the "Locations" box.
  3. Click "Attachments" from the bottom row.
  4. Select what file types you'd like to show the fields on.

Usage through PHP

The attachment location in Ultimate Fields is handled by the Ultimate_Fields\Location\Attachment class. In order to use it, you can either create the new location object manually or let the container do it for you.

The constructor of the class looks like this:

public function __construct( $file_types = array() ) {

$file_types is an array, which allows you to manually select what file types to use the location with. It expects either a single value or multiple ones, containing MIME Types.

File types are an MVIE Rule

You can add multiple values and/or exclude values by appending a minus sign in front of them.

Learn more

Manual Creation

Create a new location by using the new keyword and assign it to the container through the add_location() method.

use Ultimate_Fields\Container;
use Ultimate_Fields\Location\Attachment;

$container = Container::create( 'attachment-data' );

// Create a new location and add definitions to it
$location = new Attachment(array( 'image/jpg' ));

// Once the location has been fully set up, add it to the container
$container->add_location( $location );

Automatic creation

You can also let the container create the location for you by providing the "attachment" string as the first parameter to add_location(). The second parameter for the method would be the MIME types.

use Ultimate_Fields\Container;

Container::create( 'attachmentdata' )
	->add_location( 'attachment', array( 'image/jpeg' ) );

Data retrival

To retrieve the values of fields, associated with the attachment location, you can skip the $type parameter for *_value functions. You simply need to provide a number (int), containing the attachment ID (ex. get_value( 'copyrights_holder', 10 )).

The reason that you do not have to specifiy a particular type is that WordPress stores attachments as posts and Ultimate Fields uses the same datastore for both posts and attachments.

Example:

$copyrights_holder = get_value( 'copyrights_holder', $image->ID );
Clone this wiki locally