public

A code sample how to create custom bulk actions on the media screen in WordPress @see http://wordpress.stackexchange.com/questions/91874/how-to-make-custom-bulk-actions-work-on-the-media-upload-page/

  • Download Gist
custom_bulk_actions_on_upload.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
<?php
/*
* Since PHP5 there is no need to pass an reference of $this
*
* @see http://php.net/manual/en/language.oop5.references.php
* [...] As of PHP 5, an object variable doesn't contain the object itself as value anymore [...]
*/
add_action( 'load-upload.php', array( $this, 'custom_bulk_action' ) );
 
 
// this is a method or a function? if it is a method, don't forget
// the visibility (public, private, protected)
public function custom_bulk_action() {
/*
* if $_REQUEST['detached'] is set, then we got un-attached media. as we want to handle
* only attached medias, bail if we got un-attached medias
*/
if ( isset( $_REQUEST['detached'] ) )
return;
 
/*
* WP_Media_List_Table doesn't know anything about our actions, so it can't tell us what action was triggered
* we have to fetch the action from the $_REQUEST array
*/
// // 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;
 
// handle the top bulk action
$action_request_top = ( isset( $_REQUEST['action'] ) && ! empty( $_REQUEST['action'] ) ) ?
$_REQUEST['action'] : '';
 
// handle the bottom bulk action
$action_request_bottom = ( isset( $_REQUEST['action2'] ) && ! empty( $_REQUEST['action2'] ) ) ?
$_REQUEST['action2'] : '';
 
// select the preferred action (top or bottom)
// here we prefer the top action over the bottom action
$action = ( ! empty( $action_request_top ) ) ?
$action_request_top : $action_request_bottom;
 
// bail if no action is set or the action is not allowed
$allowed_actions = array( 'export' );
 
if ( empty( $action ) || ! in_array( $action, $allowed_actions ) )
return;
 
// security check
check_admin_referer('bulk-media');
 
/*
* yes, this is from edit.php. in edit.php we have posts, not media. media can only have the post types attachment or post
* a few lines before, we bail if the post type is not an attachment. now guess what post type we have to handle? right, attachments.
*/
// // 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 );
 
// build the query, fetch everything from the request
$query_args = array();
 
if ( isset( $_REQUEST['post_mime_type'] ) )
$query_args['post_mime_type'] = $_REQUEST['post_mime_type'];
 
if ( isset( $_REQUEST['paged'] ) )
$query_args['paged'] = $_REQUEST['paged'];
 
 
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.') );
 
// make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids'
// do array functions only on arrays. do not trust submitted data
$media = ( isset( $_REQUEST['media'] ) ) ?
(array) $_REQUEST['media'] : array();
 
if ( ! empty( $media ) ) {
 
$post_ids = array_map( 'intval', $_REQUEST['media'] );
 
if ( empty( $post_ids ) )
return;
 
}
 
$exported = 0;
foreach ( $post_ids as $post_id ) {
 
if ( ! $this->perform_export( $post_id ) )
wp_die( __('Error exporting post.') );
 
$exported++;
}
 
$query_args['exported'] = $exported;
// don't do this on GET-requests! The post_ids array could be very long and the request can cause an overflow!
// save the IDs in a transient (10 or 30 seconds lifetime) or in a session var
$query_args['ids'] = join( ',', $post_ids );
 
break;
 
default:
return;
break;
 
}
 
// we build a new query, there is no need to remove anything
//$sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback );
 
$sendback = add_query_arg( $query_args, admin_url( 'upload.php' ) );
 
wp_redirect( $sendback );
 
exit();
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.