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 found this great script from t31os that give me the chance to hide fields and buttons when we have to add images on a post in wordpress.

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 );

But one thing is missing the Use as featured image link is not appearing. Is someone have an idea on how i can make appearing the link Use as featured image beside the delete link? It is the only thing missing from this great script but i'm not a php expert i try to figure it out but didn't find the soluiton. Thank for any help.

Regards

share|improve this question
add comment

1 Answer

The 'Use as Featured' is not a part of the fields, it is a part of the form itself. You have to make sure the theme supports post-thumbnails in order for WP to do its logic and conditionally display the item.

There is not an easy way to overwrite this functionality.

Here is the code to enable post thumbnails for the theme (presumably in your functions.php file)

add_theme_support('post-thumbnails');
share|improve this answer
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.