Overview
The file advanced field uses WordPress media popup for selecting / uploading files. You can also reorder files.
Screenshot
Settings
Besides the common settings, this field has the following specific settings:
Name | Description |
---|---|
max_file_uploads |
Max number of uploaded files. Optional. |
force_delete |
Whether or not delete the files from Media Library when deleting them from post meta. true or false (default). Optional. Note: it might affect other posts if you use same file for multiple posts. |
mime_type |
MIME type of files which we want to show in Media Library. Note: this is a filter for items in Media popup, it doesn’t restrict file types when upload. |
max_status |
Display how many files uploaded/remaining. Applied only when max_file_uploads is defined. true (default) or false . Optional. |
Note that the multiple
setting is always set to true
for this field.
Sample code
array(
'id' => 'file',
'name' => 'File Advanced',
'type' => 'file_advanced',
// Delete file from Media Library when remove it from post meta?
// Note: it might affect other posts if you use same file for multiple posts
'force_delete' => false,
// Maximum file uploads.
'max_file_uploads' => 2,
// File types.
// 'mime_type' => 'application,audio,video',
// Do not show how many files uploaded/remaining.
'max_status' => 'false',
),
Data
Similar to file field, this field saves multiple values (attachment IDs) in the database. Each value (attachment ID) and is store in a single row in the database with the same meta key (similar to what add_post_meta
does with last parameter false
).
Template usage
To get the uploaded files, use the helper function rwmb_meta():
$files = rwmb_meta( 'field_id' );
foreach ( $files as $file ) {
?>
<a href="<?php echo $file['url']; ?>"><?php echo $file['name']; ?></a>
<?php
}
This helper function returns an array of files, each file has the following information:
array(
'ID' => 123,
'name' => 'intro.txt',
'path' => '/var/www/wp-content/uploads/intro.txt',
'url' => 'https://example.com/wp-content/uploads/intro.txt',
'title' => 'Introduction',
);
In case you want to get only 1 file, use the limit
parameter for the helper function:
$files = rwmb_meta( 'field_id', array( 'limit' => 1 ) );
$file = reset( $files );
?>
<a href="<?php echo $file['url']; ?>">Download File</a>
If you only want to display uploaded files in an unordered list, you can just use the rwmb_the_value():
rwmb_the_value( $field_id );
which outputs:
<ul>
<li><a href="link/to/file/">File 1</li>
<li><a href="link/to/file/">File 2</li>
</ul>
Read more about rwmb_meta() and rwmb_the_value().
Filters
This field has some filters to change the texts displayed on the screen..
Filter | Default | Description |
---|---|---|
rwmb_media_add_string |
+ Add Media | Add new file string |
rwmb_media_single_files_string |
file | Singular “file” string |
rwmb_media_multiple_files_string |
files | Plural “files” string |
rwmb_media_remove_string |
Remove | File remove string |
rwmb_media_edit_string |
Edit | File edit string |
rwmb_media_view_string |
View | File view string |
The code below changes the “+ Add Media” string:
add_filter( 'rwmb_media_add_string', 'prefix_change_add_string' );
function prefix_change_add_string() {
return '+ New File';
}