-
Notifications
You must be signed in to change notification settings - Fork 0
File
The File field allows the selection of any type of attachments in WordPress. It allows the selection of files through the media popup.
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' )
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()
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' )
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 )
);
}
Quick start
- Creating fields and using their values
- Installation
- Administration interface
- Using the PHP API
- Container Settings
Locations
- Overview & Usage
- Post Type
- Options Page
- Taxonomy
- Comment
- User
- Widget
- Shortcode
- Menu Item
- Attachment
- Customizer
Fields
- Fields
- Text
- Textarea
- WYSIWYG
- Password
- Checkbox
- Select
- Multiselect
- Image Select
- File
- Image
- Audio
- Video
- Gallery
- WP Object
- WP Objects
- Link
- Date
- DateTime
- Time
- Color
- Font
- Icon
- Map
- Embed
- Number
- Sidebar
- Complex
- Repeater
- Layout
- Section
- Tab
- Message
Features
- Adding fields to the Customizer
- Conditional Logic
- Front-End Forms
- Administration columns
- Import and Export
- REST API
- JSON Synchronization
- Yoast SEO
Ultimate Post Types
Functions and API
Tutorials