Skip to content
JIX edited this page Jul 14, 2020 · 1 revision

Purpose

The File field allows the selection of any type of attachments in WordPress. It allows the selection of files through the media popup.

file-field

Settings

Allowed file types

You can adjust what types of files are selectable for the field.

In the interface, use the "Allowed file types" field. You can select between:

  • Any file type
  • A few specific formats
  • Manual format input".

If you select the last option, there will be an additional field called "Enter file formats manually". You should enter proper MIME types like "image/jpeg" and etc.

In PHP, all formats are allowed by default. If you would like to limit the selection, please use the set_file_type( $type ) method. It expects a single or multiple MIME types, but can also work with the beginnging of a MIME type - image matches image/jpeg, image/png and etc.

Field::create( 'file', 'downloadable_file' )->set_file_type( 'application/pdf' )

Basic uploader

The basic uploader allows the File field to function without the usage of the media popup. When enabled, the file field uses an asynchronous uploader for the file, but it will still be added as a generic attachment.

The setting is only available in PHP through the use_basic_uploader method, which does not expect any parameters:

Field::create( 'file', 'resume' )->use_basic_uploader()

Output type

The following output types are supported for the_value functions:

  • link: A link to the file.
  • url: The URL of the file.
  • id: Simply the ID of the file.

In PHP you can change the output type by using the set_output_type method:

Field::create( 'file', 'downloadable_file' )->set_output_type( 'link' )

Usage

When using get_value in combination with a file field, the returned value will be the ID of the selected file if any.

Using the_value will have a different output based on the "Output Type" setting above.

// Generating a link manually
$file = get_value( 'downloadable_file' );

if( $file ) {
    sprintf(
        '<a href="%s">Download</a>',
        wp_get_attachment_url( $file )
    );
}
Clone this wiki locally