Re-order media links?

I’m trying to re-order the links in the media box when selecting an image. I want to move “Use as featured image” above the insert into post button.

I also want to rename the “Use as featured image” text? I have done this by editing the media.php file in wp-admin/incudes/media.php but I don’t want to edit this everytime I upgrade.

Read More

Is it possible to re-order the elements without having to re-write the whole function?

Thanks in advance.

EDIT:

Basically I want to move the text above the button and maybe add a label on the left too like the others above. I also want to rename the text “use as featured image”.

enter image description here

EDIT

Thanks to goto10 for helping me get this far, the code below “works” as it changes the text and location of the featured image. Although as I cannot get the attachment ID, it will not save the image…it works by manually typing in the attachment ID.

function custom_attachment_fields_to_edit($form_fields, $post) {
    $form_fields['buttons'] = array(
        'label' => 'Banner Image',
        'value' => '',
        'input' => 'html'
    );
    $thumbnail = '';
    $calling_post_id = 0;
    if (isset($_GET['post_id']))
        $calling_post_id = absint($_GET['post_id']);
    elseif (isset($_POST) && count($_POST))
        $calling_post_id = $post->post_parent;

    $attachment_id = ???        

    $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
    $form_fields['buttons']['html'] = $thumbnail = "<a class='' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail("$attachment_id", "$ajax_nonce");return false;'>Set as Banner Image</a>";

    return  $form_fields;
}

add_filter('attachment_fields_to_edit', 'custom_attachment_fields_to_edit', 11, 2); 

Tried these to get attachment ID :

$args = array('post_type' => 'attachment', 'post_parent' => $_GET['post_id'] ); 
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ( $attachments as $attachment ) {
                $attachment_id = $attachment->ID;
            }
        }

$attachment_id = get_post_meta($_GET['post_id'], '_wp_attachment_image_id', true);
$attachment_id = get_post_meta($_GET['post_id'], '_wp_attachment_url', true );

Also tried replacing $_GET['post_id'] with $calling_post_id

Any suggestions on how to get the attachment ID? I tried copying most of the code from media.php without any luck.

Related posts

Leave a Reply

1 comment

  1. 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-adminincludesmedia.php

    Most of the code that sets up the output for the buttons is on lines 1311-1342 in wp-adminincludesmedia.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.