問題描述

為了在上傳影像時簡化媒體庫佈局,有時我隱藏以下欄位:

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

問題:如何隱藏插入到帖子按鈕和設定影像文字連結。

提前致謝

最佳解決辦法

何塞,給這個鏡頭,看看這是你想到的那種事情。

編輯:完成一些核心程式碼的測試和複製,以證明這一點,但我認為下面新增的新例子應該做你所描述的,刪除按鈕,「插入到郵件」 等… ,但保留刪除連結。我已經離開了我提供的原始示例,因為它可能對某人有用。

新例子

刪除按鈕,但保留刪除連結

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

原始例子

示例如何刪除所有按鈕,但提供一個插入到 post 按鈕。

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

將過濾器設定為 priorty 15,所以在 WordPress 掛接了自己的過濾器之後掛起來 (因為它將預設優先順序為 10) 。

不確定你想要插入按鈕,只是想舉一個例子,用一個單一的插入替換原來的按鈕。

希望有幫助..

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。