Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
<?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();
}
@huykon

This comment has been minimized.

Copy link

commented Jul 4, 2017

not working with the real data

@max-kk

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.