Wordpress:删除附件字段

时间:2014-09-17 15:12:12

标签: php wordpress image media

如何删除附件字段,例如Wordpress附件媒体库中的descriptionalt

以下代码用于处理旧的Wordpress版本(3.5之前版本):

function remove_attachment_field ( $fields ) {
    unset( $fields['image_alt'] ); // Removes ALT field
    return $fields;
}
add_filter( 'attachment_fields_to_edit', 'remove_attachment_field', 15, 1 );

但从那时起,我还没有找到可行的解决方案。

有人知道解决方案吗?

澄清我要删除的字段:

enter image description here

3 个答案:

答案 0 :(得分:11)

@brasofilo的above solution应该运行良好,但我们也可以使用@EricAndrewLewis的this great answer作为如何覆盖Backbone微模板的指南

覆盖Backbone微模板 - 简短版本:

您可以使用自定义#tmpl-attachment-details覆盖微型Backbone模板#tmpl-attachment-details-custom

wp.media.view.Attachment.Details.prototype.template = wp.media.template( 'attachment-details-custom' );

同样,您可以使用以下代码覆盖微型模板#tmpl-attachment-details-two-column

#tmpl-attachment-details-two-column-custom

覆盖Backbone微模板 - 长版本:

Here您可以获取WordPress核心使用的媒体模板。

1)以下代码示例应删除 Caption Alt Text Description 字段strong>附件详细信息模板:

截图:

Modified template

代码:

wp.media.view.Attachment.Details.TwoColumn.prototype.template = wp.media.template( 'attachment-details-two-column-custom' );

2)以下代码示例应删除标题替代文字描述字段strong>附件详细信息两列模板:

截图:

Modifed template

代码:

/**
 * Override the "Attachments Details" Backbone micro template in WordPress 4.0
 *
 * @see https://stackoverflow.com/a/25948448/2078474
 */    

add_action( 'admin_footer-post.php', 'modified_attachments_details_template_so_25894288' );

function modified_attachments_details_template_so_25894288() 
{?>
        <script type="text/html" id="tmpl-attachment-details-custom">
                <h3>
                        <?php _e('Attachment Details'); ?>

                        <span class="settings-save-status">
                                <span class="spinner"></span>
                                <span class="saved"><?php esc_html_e('Saved.'); ?></span>
                        </span>
                </h3>
                <div class="attachment-info">
                        <div class="thumbnail thumbnail-{{ data.type }}">
                                <# if ( data.uploading ) { #>
                                        <div class="media-progress-bar"><div></div></div>
                                <# } else if ( 'image' === data.type && data.sizes ) { #>
                                        <img src="{{ data.size.url }}" draggable="false" />
                                <# } else { #>
                                        <img src="{{ data.icon }}" class="icon" draggable="false" />
                                <# } #>
                        </div>
                        <div class="details">
                                <div class="filename">{{ data.filename }}</div>
                                <div class="uploaded">{{ data.dateFormatted }}</div>

                                <div class="file-size">{{ data.filesizeHumanReadable }}</div>
                                <# if ( 'image' === data.type && ! data.uploading ) { #>
                                        <# if ( data.width && data.height ) { #>
                                                <div class="dimensions">{{ data.width }} &times; {{ data.height }}</div>
                                        <# } #>

                                        <# if ( data.can.save && data.sizes ) { #>
                                                <a class="edit-attachment" href="{{ data.editLink }}&amp;image-editor" target="_blank"><?php _e( 'Edit Image' ); ?></a>
                                               <a class="refresh-attachment" href="#"><?php _e( 'Refresh' ); ?></a>
                                        <# } #>
                                <# } #>

                                <# if ( data.fileLength ) { #>
                                        <div class="file-length"><?php _e( 'Length:' ); ?> {{ data.fileLength }}</div>
                                <# } #>

                                <# if ( ! data.uploading && data.can.remove ) { #>
                                        <?php if ( MEDIA_TRASH ): ?>
                                        <# if ( 'trash' === data.status ) { #>
                                                <a class="untrash-attachment" href="#"><?php _e( 'Untrash' ); ?></a>
                                        <# } else { #>
                                                <a class="trash-attachment" href="#"><?php _e( 'Trash' ); ?></a>
                                        <# } #>
                                        <?php else: ?>
                                                <a class="delete-attachment" href="#"><?php _e( 'Delete Permanently' ); ?></a>
                                        <?php endif; ?>
                                <# } #>

                                <div class="compat-meta">
                                        <# if ( data.compat && data.compat.meta ) { #>
                                                {{{ data.compat.meta }}}
                                        <# } #>
                                </div>
                        </div>
                </div>

                <label class="setting" data-setting="url">
                        <span class="name"><?php _e('URL'); ?></span>
                        <input type="text" value="{{ data.url }}" readonly />
                </label>
                <# var maybeReadOnly = data.can.save || data.allowLocalEdits ? '' : 'readonly'; #>
                <label class="setting" data-setting="title">
                        <span class="name"><?php _e('Title'); ?></span>
                        <input type="text" value="{{ data.title }}" {{ maybeReadOnly }} />
                </label>
                <# if ( 'audio' === data.type ) { #>
                <?php foreach ( array(
                        'artist' => __( 'Artist' ),
                        'album' => __( 'Album' ),
                ) as $key => $label ) : ?>
                <label class="setting" data-setting="<?php echo esc_attr( $key ) ?>">
                        <span class="name"><?php echo $label ?></span>
                        <input type="text" value="{{ data.<?php echo $key ?> || data.meta.<?php echo $key ?> || '' }}" />
                </label>
                <?php endforeach; ?>
                <# } #>
<!-- LET'S REMOVE THIS SECTION:
                <label class="setting" data-setting="caption">
                        <span class="name"><?php _e('Caption'); ?></span>
                        <textarea {{ maybeReadOnly }}>{{ data.caption }}</textarea>
                </label>
                <# if ( 'image' === data.type ) { #>
                        <label class="setting" data-setting="alt">
                                <span class="name"><?php _e('Alt Text'); ?></span>
                                <input type="text" value="{{ data.alt }}" {{ maybeReadOnly }} />
                        </label>
                <# } #>
                <label class="setting" data-setting="description">
                        <span class="name"><?php _e('Description'); ?></span>
                        <textarea {{ maybeReadOnly }}>{{ data.description }}</textarea>
                </label>
-->
  </script>
  <script>
    jQuery(document).ready( function($) {
        if( typeof wp.media.view.Attachment.Details != 'undefined' ){
            wp.media.view.Attachment.Details.prototype.template = wp.media.template( 'attachment-details-custom' );
        }
    });
    </script>
    <?php
}

您可以根据需要修改它。

答案 1 :(得分:8)

可以使用admin_print_styles-$pageselecting the elements using the data attribute/wp-admin/post.php上打印样式。


可以检测当前的帖子类型,并仅对给定的类型应用规则:

foreach( array( 'post.php', 'post-new.php' ) as $hook )
    add_action( "admin_print_styles-$hook", 'admin_styles_so_25894288');

function admin_styles_so_25894288() {
    global $typenow;
    if( 'post' !== $typenow )
        return;
    ?>
    <style>
        .media-sidebar .setting[data-setting="caption"],
        .media-sidebar .setting[data-setting="description"],
        .media-sidebar .setting[data-setting="alt"] { 
            display: none; 
        }
    </style>
    <?php
}

答案 2 :(得分:1)

试试这个:

add_action('admin_head', 'remove_attachment_field');
function remove_attachment_field() {
  echo "<style>div.attachment-info label.setting[data-setting=alt], div.attachment-info label.setting[data-setting=caption], div.attachment-info label.setting[data-setting=description] { display: none; }</style>";
}