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.
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”.
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.
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 theattachment_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:
Notes:
The filter
attachment_fields_to_edit
is applied online 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 aboveline 1311
that are used to determine how the output is made which are not passed into theattachment_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.