php – WordPress:删除附件字段
发布时间:2020-12-13 17:35:25 所属栏目:PHP教程 来源:网络整理
导读:如何删除附件字段,如Wordpress附件的媒体库中的描述和alt? 以下代码用于旧版Wordpress版本(3.5之前版本): function remove_attachment_field ( $fields ) { unset( $fields['image_alt'] ); // Removes ALT field return $fields;}add_filter( 'attachment
如何删除附件字段,如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 }} × {{ data.height }}</div> <# } #> <# if ( data.can.save && data.sizes ) { #> <a class="edit-attachment" href="{{ data.editLink }}&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 }} × {{ 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 } 您可以希望根据您的需要修改此信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |