Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

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.

share|improve this question
1  
Take a look at the filters and actions in the core files you edited. +1 for "not editing core because of upgrades". –  kaiser Sep 2 '11 at 16:21
    
I have used the filter to modify the title of the meta box, although there doesn't seem to be on for the media that I can see. –  Elliott Sep 2 '11 at 19:44
add comment

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
Thanks! Updated my post, everything upto now works great but am having trouble getting the attachment id. –  Elliott Sep 5 '11 at 10:58
    
No problem, @Elliot. I'm a big fan of being able to customize the built in WP galleries. I edited my answer, adding an example of outputting the attachment's id. –  goto10 Sep 5 '11 at 16:59
    
Doh! Didn't realise how easy it was to get the attachment id. Just one quick question is it possible to do conditional tags in functions.php? Such as if the is_page('1)' : do something? Thanks alot =D –  Elliott Sep 5 '11 at 18:35
    
No prob. Yes, you can use conditional tags in functions.php, but you'd want to do so from within a hook. E.g., add_filter( 'the_content', 'customize_content'); function customize_content( $content ) { if ( is_page(675) ) { return "<h1>--- TEST ---</h1>$content<h1>--- END TEST ---</h1>"; } } // See: codex.wordpress.org/Conditional_Tags –  goto10 Sep 5 '11 at 19:10
    
Thanks again :) –  Elliott Sep 5 '11 at 20:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.