diff --git a/js/media/fieldmanager-media.js b/js/media/fieldmanager-media.js
index 2ac367b534..667b92d91a 100644
--- a/js/media/fieldmanager-media.js
+++ b/js/media/fieldmanager-media.js
@@ -28,6 +28,12 @@ $( document ).on( 'click', '.fm-media-button', function( event ) {
library.type = $el.data( 'mime-type' );
}
+ // Add query arguments to the media window request.
+ // Gives "data-mime-type" priority over "mime-type" defined in "data-queryargs".
+ if ( $el.data( 'queryargs' ) ) {
+ library = $.extend( {}, $el.data( 'queryargs' ), library );
+ }
+
// Create the media frame.
fm_media_frame[ $el.attr('id') ] = wp.media({
// Set the library attributes.
diff --git a/php/class-fieldmanager-media.php b/php/class-fieldmanager-media.php
index 77e7744afe..7d174ae9c9 100644
--- a/php/class-fieldmanager-media.php
+++ b/php/class-fieldmanager-media.php
@@ -85,6 +85,13 @@ class Fieldmanager_Media extends Fieldmanager_Field {
*/
public $mime_type = 'all';
+ /**
+ * Query arguments to pass to wp.media window.
+ *
+ * @var array
+ */
+ public $queryargs = array();
+
/**
* Static variable so we only load media JS once.
*
@@ -174,7 +181,7 @@ public function form_element( $value = array() ) {
$preview = '';
}
return sprintf(
- '
+ '
%5$s
',
esc_attr( $this->get_element_id() ),
@@ -185,6 +192,7 @@ public function form_element( $value = array() ) {
esc_attr( $this->preview_size ),
esc_attr( $this->modal_title ),
esc_attr( $this->modal_button_label ),
+ esc_attr( wp_json_encode( $this->queryargs ) ),
esc_attr( $this->mime_type ),
$this->get_element_attributes()
);
diff --git a/tests/php/test-fieldmanager-media-field.php b/tests/php/test-fieldmanager-media-field.php
index 6f2c1a12f6..81de28be9c 100644
--- a/tests/php/test-fieldmanager-media-field.php
+++ b/tests/php/test-fieldmanager-media-field.php
@@ -34,6 +34,7 @@ public function test_basic_render() {
'selected_file_label' => rand_str(),
'remove_media_label' => rand_str(),
'preview_size' => rand_str(),
+ 'queryargs' => rand_str(),
);
$fm = new Fieldmanager_Media( $args );
$context = $fm->add_meta_box( 'Test Media', 'post' );
@@ -43,11 +44,12 @@ public function test_basic_render() {
$html = ob_get_clean();
$this->assertRegExp(
sprintf(
- '#]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-mime-type="all" */>#',
+ '#]+value="%s" data-choose="%s" data-update="%s" data-preview-size="%s" data-queryargs="%s" data-mime-type="all" */>#',
$args['button_label'],
$args['modal_title'],
$args['modal_button_label'],
- $args['preview_size']
+ $args['preview_size'],
+ $args['queryargs']
),
$html
);