Skip to content

Instantly share code, notes, and snippets.

@imjjss
Created May 20, 2012 14:14
Show Gist options
  • Save imjjss/2758270 to your computer and use it in GitHub Desktop.
Save imjjss/2758270 to your computer and use it in GitHub Desktop.
Media Library Menu Check
<?php
//http://shibashake.com/wordpress-theme/expand-the-wordpress-media-library-admin-panel
add_action('admin_init', 'init_media_plus');
function init_media_plus() {
// Only activate plugin for the Media Library page
if (strpos($_SERVER["REQUEST_URI"], "upload.php") === FALSE)
return;
add_action('restrict_manage_posts', 'media_library_menu');
add_action('admin_head', 'media_plus_header', 51);
add_filter('wp_redirect', 'media_plus_redirect', 10, 2);
}
function media_library_menu() {
global $wpdb;
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Media Library Plus</h2>
<form id="media-plus-form" action="" method="get">
<select name="action" class="select-action">
<option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
<option value="delete"><?php _e('Delete'); ?></option>
<option value="attach"><?php _e('Attach to a post'); ?></option>
<option value="remove"><?php _e('Detach from a post'); ?></option>
</select>
<input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction" id="doaction" class="button-secondary action" onClick="getSelectedMedia('posts-filter');" />
<?php wp_nonce_field('bulk-media'); ?>
<?php find_posts_div(); ?>
</form>
</div> <!-- End div wrap -->
<?php
}
//add the selected media objects into our new Media Library form by using Javascript
function media_plus_header() {
?>
<script type="text/javascript">
<!--
// get_selected_media(document.posts-filter.list)
function addNewArg(name, value) {
var newArg = document.createElement("input");
newArg.type = "hidden";
newArg.name = name;
newArg.value = value;
newArg.id = name;
return newArg;
}
function getSelectedMedia() {
var mediaPlusForm=document.getElementById('media-plus-form' );
var args = document.getElementById('posts-filter').elements;
for (i = 0; i < args.length; i++) {
if (args[i].name == "media[]" && args[i].checked) {
mediaPlusForm.appendChild(addNewArg("media[]", args[i].value));
}
}
// Redirect to previous page by adding previous mime_type and detached state
var hasType = location.href.indexOf('post_mime_type');
if (hasType >= 0) {
var sPos = location.href.indexOf('=', mimeType);
var ePos = location.href.indexOf('&', sPos);
if (ePos >= 0) {
var mimeStr = location.href.substring(sPos+1, ePos);
} else {
var mimeStr = location.href.substring(sPos+1);
}
mediaPlusForm.appendChild(addNewArg('post_mime_type', mimeStr));
}
if (location.href.indexOf('detached') >= 0) {
mediaPlusForm.appendChild(addNewArg('detached', '1'));
}
}
//-->
</script>
<?php
}
// Change redirect set in upload.php
function media_plus_redirect($location, $status) {
if ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
if (!isset($_GET['detached']))
$location = remove_query_arg('detached', $location);
}
return $location;
//the current WordPress Media Library does not allow for the removal of attachments //from media objects. To enable this new option, we add a new function
function detach_media_action() {
// $_GET['doaction'] should be 'Apply'
global $wpdb;
if ( !strcmp($_GET['action'], 'remove') && isset($_GET['media']) ) {
check_admin_referer('bulk-media');
$attach = array();
foreach( (array) $_GET['media'] as $att_id ) {
$att_id = (int) $att_id;
if ( !current_user_can('edit_post', $att_id) )
continue;
$attach[] = $att_id;
}
if ( ! empty($attach) ) {
$attach = implode(',', $attach);
$attached = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ($attach)", '') );
}
if ( isset($attached) ) {
$location = 'upload.php';
if ( $referer = wp_get_referer() ) {
if ( false !== strpos($referer, 'upload.php') )
$location = $referer;
}
$location = add_query_arg( array( 'attached' => $attached ) , $location );
// redirect after header here can't use wp_redirect($location);
?>
<script type="text/javascript">
<!--
window.location= <?php echo "'" . $location . "'"; ?>;
//-->
</script>
<?php
exit;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment