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:
- What is the media analog of
load-edit.php
? - Where is
load-edit.php
(and the otherload-*.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!
add_action('load-upload.php', 'callback');
works ok.add_action('load-upload.php', 'my_callback'); function my_callback() { wp_die('hook firing ok'); }
upload.php
) page--this seems appropriate. So we know the hook is working. I'm going to paste my code above and perhaps you can tell me why the global variable isn't coming through correctly.