问题描述

为了在上传图像时简化媒体库布局,有时我隐藏以下字段:

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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。