Expanding the media library select options
-
I’m trying to add an option to the media library select dropdown that will allow the user to view only images uploaded by a particular author – I don’t want this to definitively be the case, I want the option via the select box.
Right now I’ve overridden AttachmentFilters.All’s createFilters function to add my own filter like so:
add_action( 'admin_print_footer_scripts', 'add_author_to_filter_object', 51); function add_author_to_filter_object() { ?> <script type="text/javascript"> // Add custom post type filters l10n = wp.media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n; wp.media.view.AttachmentFilters.All.prototype.createFilters = function() { var filters = {}; //new one filters.featuredAuthor = { text: 'Images Uploaded By Featured Author', props: { status: null, type: null, uploadedTo: null, orderby: 'date', order: 'DESC', featuredAuthor: true }, priority: 5 }; _.each( wp.media.view.settings.mimeTypes || {}, function( text, key ) { filters[ key ] = { text: text, props: { status: null, type: key, uploadedTo: null, orderby: 'date', order: 'DESC' } }; }); filters.all = { text: l10n.allMediaItems, props: { status: null, type: null, uploadedTo: null, orderby: 'date', order: 'DESC' }, priority: 10 }; if ( wp.media.view.settings.post.id ) { filters.uploaded = { text: l10n.uploadedToThisPost, props: { status: null, type: null, uploadedTo: wp.media.view.settings.post.id, orderby: 'menuOrder', order: 'ASC' }, priority: 20 }; } filters.unattached = { text: l10n.unattached, props: { status: null, uploadedTo: 0, type: null, orderby: 'menuOrder', order: 'ASC' }, priority: 50 }; if ( wp.media.view.settings.mediaTrash && this.controller.isModeActive( 'grid' ) ) { filters.trash = { text: l10n.trash, props: { uploadedTo: null, status: 'trash', type: null, orderby: 'date', order: 'DESC' }, priority: 50 }; } this.filters = filters; }; </script> <?php }
Which adds the option to the select box as expected – great. Now I want to catch the ajax query and adjust its parameters accordingly:
add_filter('ajax_query_attachments_args', 'featured_author_media_filter'); public static function featured_author_media_filter($query) { if($query['featuredAuthor']) { $featured_author = get_field('featured_author', 2); //get from front page $query['author'] = $featured_author->post_author; } return $query; }
This is where things go poorly. It seems like my
if
statement there is never true. If I do a dump or something before it, the ajax loading of the media library fails, so I know at least my filter is being called. Is there something I’m missing? I don’t feel like this whole thing is very well documented and I admittedly know next-to-nothing about Backbone.
- The topic ‘Expanding the media library select options’ is closed to new replies.