Re-order media links?

Edit: Added example of outputting the attachment’s id. Assigned it to the variable $attachment_id, since that’s how the core code refers to it. Note that the $post object (for the attachment) is passed into the callback of the attachment_fields_to_edit filter, so you’ll have access to all of the attachment’s properties.

Yes, this can be done without modifying the core. attachment_fields_to_edit is the filter that you need here.

Add this to your functions.php or plugin:

add_filter( 'attachment_fields_to_edit', 'customize_attachment_fields_to_edit', 11, 2 ); // Note priority 11 to ensure that the customizations are not overridden
    function customize_attachment_fields_to_edit( $form_fields, $post ) {
        $form_fields['buttons'] = array(
                'label' => '',
                'value' => '',
                'input' => 'html'
        );
        $attachment_id = $post->ID;
        $form_fields['buttons']['html'] = "<h1>Custom stuff here... Attachment ID: $attachment_id</h1>";

        return  $form_fields;
    }

Notes:
The filter attachment_fields_to_edit is applied on line 1147 in \wp-admin\includes\media.php

Most of the code that sets up the output for the buttons is on lines 1311-1342 in \wp-admin\includes\media.php, although there are some variables above line 1311 that are used to determine how the output is made which are not passed into the attachment_fields_to_edit filter.

Essentially, you’re going to want to copy the core code and add it to your customize_attachment_fields_to_edit callback. Then format the copied code to suit your needs, but keep in mind that you might need to create some of the vars on your own ($send, for example, if you really want to duplicate the core code as close as possible).

Here’s a link to a well written tutorial by Andy Blackwell on customizing WP galleries.

Leave a Comment