I had a very similar question (“How to make custom bulk actions work on the media/upload page?“) about Justin Stern’s Custom Bulk Action Demo, which was just answered.
My biggest piece of advice to you is to take the adaptation one step at a time: make sure you have export
working for media before anything else. Don’t try to do two steps at once. (That’s the secret to all this computer stuff: break things into small, manageable pieces.)
For your specific questions at the end:
- Replacing
check_admin_referer('bulk-posts')
withcheck_admin_referer('bulk-media')
will get rid of “Are you sure you want to do this?”. - The hook
add_action('load-upload.php', 'custom_bulk_action')
may sound silly, but is fine. $sendback = admin_url( "upload.php" )
is fine.
In part 1, custom_bulk_admin_footer()
, and part 3, custom_bulk_admin_notices()
, you want to replace
'post'
–>'attachment'
.
Elsewhere you need to replace
post.php
–>upload.php
,edit
–>upload
, andpost
–>media
.
Part 2, custom_bulk_action()
, requires more-complicated adaptations. I recommend you use the code below (from the question linked above).
add_action('load-upload.php', array(&$this, 'custom_bulk_action'));
function custom_bulk_action() {
// ***if($post_type == 'attachment') { REPLACE WITH:
if ( !isset( $_REQUEST['detached'] ) ) {
// get the action
$wp_list_table = _get_list_table('WP_Media_List_Table');
$action = $wp_list_table->current_action();
echo "\naction = $action\n</pre>";
$allowed_actions = array("export");
if(!in_array($action, $allowed_actions)) return;
// security check
// ***check_admin_referer('bulk-posts'); REPLACE WITH:
check_admin_referer('bulk-media');
// make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids'
if(isset($_REQUEST['media'])) {
$post_ids = array_map('intval', $_REQUEST['media']);
}
if(empty($post_ids)) return;
// this is based on wp-admin/edit.php
$sendback = remove_query_arg( array('exported', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
if ( ! $sendback )
$sendback = admin_url( "upload.php?post_type=$post_type" );
$pagenum = $wp_list_table->get_pagenum();
$sendback = add_query_arg( 'paged', $pagenum, $sendback );
switch($action) {
case 'export':
// if we set up user permissions/capabilities, the code might look like:
//if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
// wp_die( __('You are not allowed to export this post.') );
$exported = 0;
foreach( $post_ids as $post_id ) {
if ( !$this->perform_export($post_id) )
wp_die( __('Error exporting post.') );
$exported++;
}
$sendback = add_query_arg( array('exported' => $exported, 'ids' => join(',', $post_ids) ), $sendback );
break;
default: return;
}
$sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback );
wp_redirect($sendback);
exit();
}
}