How to make custom bulk actions work on the media/upload page?

I’m adapting a script I found online to add custom bulk actions to the screen with the list of posts. It has this line:

add_action('load-edit.php', 'custom_bulk_action');

I’m trying to adapt it for the media library. I see that in place of edit.php I should use upload.php, which leads me to believe I need to find the media analog for load-edit.php. Sounds easy, but I can’t even find load-edit.php in my WP install to see if by chance it might be what I’m looking for itself. I have found a few references online to load-*.php (e.g., Custom bulk_action), but nothing that tells me what values * can take.

(I’ve tried load-upload.php but it’s not working–though it could always be something else in my code that’s gumming up the works.)

So my questions are two:

  1. What is the media analog of load-edit.php?
  2. Where is load-edit.php (and the other load-*.php files), or what code handles these file requests?

The first is my real question, but the second has gotten under my skin.

Can any of you experts out there give me some guidance? I would very much appreciate it.

EDIT

By “not working” I meant not that it crashes, but that it wasn’t doing as it was supposed (changing a media attribute).

The code I’m adapting can be downloaded at the bottom of the post “Add a WordPress Custom Bulk Action” by Justin Stern of Fox Run Software. Going back to verify each step of the code, I got the adapted version to work, but only if I comment out the conditional and the security check (both asterisked below). What are the media analogs I should use to replace these?

 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();
  }
}

I appreciate your help.

EDIT 2

I modified the code above to reflect information from the accepted answer. Many thanks to Ralf912!

2 Answers
2

If you want to use your code, try this:

If you want to check if the medias are attachments, you can try to use $_REQUEST['detached']

add_action( 'load-upload.php', 'export_media_test' );

function export_media_test() {

    if ( ! isset( $_REQUEST['action'] ) )
        return;

    echo 'Export Media';

    if ( isset( $_REQUEST['detached'] ) ) {
        die( 'No attachments' );
    } else {
        die( 'Attachments' );
    }

    exit();
}

You can not check an nonce that wasn’t set. The nonce bulk-posts is set in edit.php, and this is the the posts list. In upload.php is the bulk-media nonce set. So use check_admin_referer('bulk-media');

Leave a Comment