6

To simplify the Media Library layout to users when uploading an image, sometimes i hide the following fields:

function myAttachmentFields($form_fields, $post) {

    if ( substr($post->post_mime_type, 0, 5) == 'image' ) {


 $form_fields['image_alt']['value'] = '';
 $form_fields['image_alt']['input'] = 'hidden';

 $form_fields['post_excerpt']['value'] = '';
 $form_fields['post_excerpt']['input'] = 'hidden';

 $form_fields['post_content']['value'] = '';
 $form_fields['post_content']['input'] = 'hidden';

 $form_fields['url']['value'] = '';
 $form_fields['url']['input'] = 'hidden';

 $form_fields['align']['value'] = 'aligncenter';
 $form_fields['align']['input'] = 'hidden';

 $form_fields['image-size']['value'] = 'thumbnail';
 $form_fields['image-size']['input'] = 'hidden';

 $form_fields['image-caption']['value'] = 'caption';
 $form_fields['image-caption']['input'] = 'hidden';

    }

    return $form_fields;

}

add_filter('attachment_fields_to_edit', 'myAttachmentFields');

Question: How do I hide the Insert into post button and the Set image text link.

Thanks in advance

  • I think the only way to do this is by removing the button tag trough JavaScript :( – onetrickpony Dec 20 '10 at 2:38
  • I just do it using: $form_fields['buttons']['value'] = ''; $form_fields['buttons']['input'] = 'hidden'; – José Pablo Orozco Marín Dec 20 '10 at 2:51
  • But i need to preserve the Delete option. – José Pablo Orozco Marín Dec 20 '10 at 2:51
  • $form_fields is empty in WP 3.5+ – That Brazilian Guy Apr 25 '17 at 16:31
2

Jose, give this a shot and see if that's the kind of thing you had in mind..

EDIT: Done a little bit of testing and copying of core code to archieve this, but i think the new example i've added below should do what you've described, remove the buttons, "Insert into Post", etc..., but keep a delete link. I've left the original example i provided because it may be useful to someone.

New Example

Remove buttons, but keep a delete link

function myAttachmentFields($form_fields, $post) {
    // Can now see $post becaue the filter accepts two args, as defined in the add_fitler
    if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
        $form_fields['image_alt']['value'] = '';
        $form_fields['image_alt']['input'] = 'hidden';

        $form_fields['post_excerpt']['value'] = '';
        $form_fields['post_excerpt']['input'] = 'hidden';

        $form_fields['post_content']['value'] = '';
        $form_fields['post_content']['input'] = 'hidden';

        $form_fields['url']['value'] = '';
        $form_fields['url']['input'] = 'hidden';

        $form_fields['align']['value'] = 'aligncenter';
        $form_fields['align']['input'] = 'hidden';

        $form_fields['image-size']['value'] = 'thumbnail';
        $form_fields['image-size']['input'] = 'hidden';

        $form_fields['image-caption']['value'] = 'caption';
        $form_fields['image-caption']['input'] = 'hidden';

        $form_fields['buttons'] = array(
            'label' => '',
            'value' => '',
            'input' => 'html'
        );
        $filename = basename( $post->guid );
        $attachment_id = $post->ID;
        if ( current_user_can( 'delete_post', $attachment_id ) ) {
            if ( !EMPTY_TRASH_DAYS ) {
                $form_fields['buttons']['html'] = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' ) . '</a>';
            } elseif ( !MEDIA_TRASH ) {
                $form_fields['buttons']['html'] = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
                         <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "
                         <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
                         <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
                         </div>";
            } else {
                $form_fields['buttons']['html'] = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a><a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-attachment_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
            }
        }
        else {
            $form_fields['buttons']['html'] = '';
        }
    }
    return $form_fields;
}
// Hook on after priority 10, because WordPress adds a couple of filters to the same hook - added accepted args(2)
add_filter('attachment_fields_to_edit', 'myAttachmentFields', 11, 2 );

Original Example

Example of how to remove all buttons, but provide an insert into post button.

function myAttachmentFields($form_fields, $post) {

    if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {

        // .. $form_fields code here(per above example), trimmed for illustration ..

        $form_fields['buttons'] = array(
            'label' => '', // Put a label in?
            'value' => '', // Doesn't need one
            'html' => "<input type='submit' class='button' name='send[$post->ID]' value='" . esc_attr__( 'Insert into Post' ) . "' />",
            'input' => 'html'
        );
    }
    return $form_fields;
}
// Hook on after priority 10, because WordPress adds a couple of filters to the same hook
add_filter('attachment_fields_to_edit', 'myAttachmentFields', 15, 2 );

Set the filter to priorty 15, so it's hooked on after WordPress has hooked it's own filters on(because it adds them at the default priority of 10).

Not sure you wanted the insert button, just wanted to give an example of replacing the original buttons with a singular insert one..

Hope that helps..

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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