微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – WordPress:删除附件字段

如何删除附件字段,如wordpress附件的媒体库中的描述和alt?

以下代码用于旧版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 );

但从那以后,我还没有找到一个有效的解决方案.

有人知道解决方案吗?

澄清我想删除的字段:

@brasofilo的 above solution应该能够正常工作,但是我们也可以使用@EricAndrewLewis的 this great answer作为如何覆盖Backbone微模板的指导.

覆盖骨干微模板 – 简短版本:

您可以使用以下命令覆盖您的自定义#tmpl-attachment-details-custom自定义的micro Backbone模板#tmpl-attachment-details:

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

同样,您可以使用#tmpl-attachment-details-two-column-custom来覆盖微模板#tmpl-attachment-details-two-column:

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

覆盖骨干微模板 – 长版本:

Here您可以获取wordpress核心使用的媒体模板.

1)以下代码示例应从“附件详细信息”模板中删除Caption,Alt Text和Description字段:

截图:

码:

/**
 * 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
}

2)以下代码示例应从“附件详细信息两列”模板中删除Caption,Alt Text和Description字段:

截图:

码:

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

add_action( 'admin_footer-upload.PHP','modified_attachments_details_two_column_template_so_25894288' );

function modified_attachments_details_two_column_template_so_25894288() 
{ ?>
        <script type="text/html" id="tmpl-attachment-details-two-column-custom">
                <div class="attachment-media-view {{ data.orientation }}">
                        <div class="thumbnail thumbnail-{{ data.type }}">
                                <# if ( data.uploading ) { #>
                                        <div class="media-progress-bar"><div></div></div>
                                <# } else if ( 'image' === data.type && data.sizes && data.sizes.large ) { #>
                                        <img class="details-image" src="{{ data.sizes.large.url }}" draggable="false" />
                                <# } else if ( 'image' === data.type && data.sizes && data.sizes.full ) { #>
                                        <img class="details-image" src="{{ data.sizes.full.url }}" draggable="false" />
                                <# } else if ( -1 === jQuery.inArray( data.type,[ 'audio','video' ] ) ) { #>
                                        <img class="details-image" src="{{ data.icon }}" class="icon" draggable="false" />
                                <# } #>

                                <# if ( 'audio' === data.type ) { #>
                                <div class="wp-media-wrapper">
                                        <audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
                                                <source type="{{ data.mime }}" src="{{ data.url }}"/>
                                        </audio>
                                </div>
                                <# } else if ( 'video' === data.type ) {
                                        var w_rule = h_rule = '';
                                        if ( data.width ) {
                                                w_rule = 'width: ' + data.width + 'px;';
                                        } else if ( wp.media.view.settings.contentWidth ) {
                                                w_rule = 'width: ' + wp.media.view.settings.contentWidth + 'px;';
                                        }
                                        if ( data.height ) {
                                                h_rule = 'height: ' + data.height + 'px;';
                                        }
                                #>
                                <div style="{{ w_rule }}{{ h_rule }}" class="wp-media-wrapper wp-video">
                                        <video controls="controls" class="wp-video-shortcode" preload="Metadata"
                                                <# if ( data.width ) { #>width="{{ data.width }}"<# } #>
                                                <# if ( data.height ) { #>height="{{ data.height }}"<# } #>
                                                <# if ( data.image && data.image.src !== data.icon ) { #>poster="{{ data.image.src }}"<# } #>>
                                                <source type="{{ data.mime }}" src="{{ data.url }}"/>
                                        </video>
                                </div>
                                <# } #>

                                <div class="attachment-actions">
                                        <# if ( 'image' === data.type && ! data.uploading && data.sizes && data.can.save ) { #>
                                                <a class="button edit-attachment" href="#"><?PHP _e( 'Edit Image' ); ?></a>
                                        <# } #>
                                </div>
                        </div>
                </div>
                <div class="attachment-info">
                        <span class="settings-save-status">
                                <span class="spinner"></span>
                                <span class="saved"><?PHP esc_html_e('Saved.'); ?></span>
                        </span>
                        <div class="details">
                                <div class="filename"><strong><?PHP _e( 'File name:' ); ?></strong> {{ data.filename }}</div>
                                <div class="filename"><strong><?PHP _e( 'File type:' ); ?></strong> {{ data.mime }}</div>
                                <div class="uploaded"><strong><?PHP _e( 'Uploaded on:' ); ?></strong> {{ data.dateFormatted }}</div>

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

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

                                <# if ( 'audio' === data.type && data.Meta.bitrate ) { #>
                                        <div class="bitrate">
                                                <strong><?PHP _e( 'Bitrate:' ); ?></strong> {{ Math.round( data.Meta.bitrate / 1000 ) }}kb/s
                                                <# if ( data.Meta.bitrate_mode ) { #>
                                                {{ ' ' + data.Meta.bitrate_mode.toupperCase() }}
                                                <# } #>
                                        </div>
                                <# } #>

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

                        <div class="settings">
                                <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' ),) 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 xxx' ); ?></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 xxx'); ?></span>
                                        <textarea {{ maybeReadOnly }}>{{ data.description }}</textarea>
                                </label>
                                <label class="setting">
                                        <span class="name"><?PHP _e( 'Uploaded By' ); ?></span>
                                        <span class="value">{{ data.authorName }}</span>
                                </label>
                                <# if ( data.uploadedToTitle ) { #>
                                        <label class="setting">
                                                <span class="name"><?PHP _e( 'Uploaded To' ); ?></span>
                                                <# if ( data.uploadedToLink ) { #>
                                                        <span class="value"><a href="{{ data.uploadedToLink }}">{{ data.uploadedToTitle }}</a></span>
                                                <# } else { #>
                                                        <span class="value">{{ data.uploadedToTitle }}</span>
                                                <# } #>
                                        </label>
                                <# } #>
-->
                                <div class="attachment-compat"></div>
                        </div>

                        <div class="actions">
                                <a class="view-attachment" href="{{ data.link }}"><?PHP _e( 'View attachment page' ); ?></a>
                                <# if ( data.can.save ) { #> |
                                        <a href="post.PHP?post={{ data.id }}&action=edit"><?PHP _e( 'Edit more details' ); ?></a>
                                <# } #>
                                <# 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>

                </div>
        </script>
  <script>
    jQuery(document).ready( function($) {
        if( typeof wp.media.view.Attachment.Details.TwoColumn != 'undefined' ){
            wp.media.view.Attachment.Details.TwoColumn.prototype.template = wp.template( 'attachment-details-two-column-custom' );
        }
    });
    </script>
    <?PHP
}

您可以希望根据您的需要修改此信息.

原文地址:https://www.jb51.cc/php/131436.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐