Last active
October 5, 2018 18:09
-
-
Save bitfade/4476771 to your computer and use it in GitHub Desktop.
WordPress 3.5 media upload, toolbar with custom filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint undef: false, browser: true, devel: false, eqeqeq: false, bitwise: false, white: false, plusplus: false, regexp: false, nomen: false */ | |
/*global wp,jQuery */ | |
jQuery(document).ready(function($) { | |
if (!window.wp || !window.wp.media) { | |
return; | |
} | |
var media = window.wp.media; | |
// array of tag names | |
var names = window.pe_theme_media_filters.names; | |
// array of tag slugs | |
var slugs = window.pe_theme_media_filters.slugs; | |
var tagFilter = media.view.AttachmentFilters.extend({ | |
createFilters: function() { | |
var filters = {}; | |
// default "all" filter, shows all tags | |
filters.all = { | |
text: window.pe_theme_media_filters.all, | |
props: { | |
// unset tag | |
tag: null, | |
type: null, | |
uploadedTo: null, | |
orderby: 'date', | |
order: 'DESC' | |
}, | |
priority: 10 | |
}; | |
// create a filter for each tag | |
var i; | |
for (i = 0;i<names.length;i++) { | |
filters[slugs[i]] = { | |
// tag name | |
text: names[i], | |
props: { | |
// tag slug | |
tag: slugs[i], | |
type: null, | |
uploadedTo: null, | |
orderby: 'date', | |
order: 'DESC' | |
}, | |
priority: 20+i | |
}; | |
} | |
this.filters = filters; | |
} | |
}); | |
// backup the method | |
var orig = wp.media.view.AttachmentsBrowser; | |
wp.media.view.AttachmentsBrowser = wp.media.view.AttachmentsBrowser.extend({ | |
createToolbar: function() { | |
// call the original method | |
orig.prototype.createToolbar.apply(this,arguments); | |
// add our custom filter | |
this.toolbar.set('tags', new tagFilter({ | |
controller: this.controller, | |
model: this.collection.props, | |
// controls the position, left align if < 0, right align otherwise | |
priority: -10 | |
}).render() ); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// high prio so it gets executed before the native function | |
add_action('wp_ajax_query-attachments', 'my_wp_ajax_query_attachments',1); | |
function my_wp_ajax_query_attachments() { | |
add_filter('pre_get_posts', 'my_pre_get_posts_filter'); | |
} | |
function my_pre_get_posts_filter($query) { | |
if ($mtag = empty($_REQUEST['query']['tag']) ? false : $_REQUEST['query']['tag']) { | |
// add our filter value to query parameters | |
$query->query_vars["media-tag"] = $mtag; | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code doesn't display anything, please help.