zoukankan      html  css  js  c++  java
  • 黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素

    原文:http://www.solagirl.net/customize-wordpress-media-upload-ui.html

    WordPress编写文章界面的添加媒体按钮允许用户上传多媒体文件,但并不是每个人都能用的顺手,有的人倾向于引用外部图片,所以希望“从URL上传”这一项是默认选中的,有的人喜欢从媒体库中挑选图片,如果定制一下WordPress添加媒体界面,就能称心如意了。

    注:该代码只适用于<3.5的版本,3.5以上定制方法请看文章最后一节。

    定制WordPress添加媒体界面

    WordPress的好处就是提供了很多钩子函数(actions and filters),使用户不用更改核心文件就能改变很多WordPress的默认行为,媒体界面也不例外。

    更改多媒体选项卡位置

    多媒体选型卡通常有四项:从计算机上传、从URL上传、从相册上传(如果该文章已经有图片附件)和从媒体库选择图片。

    改变它们的顺序只需要使用filter: media_upload_tabs。将下面的代码放到主题的functions.php中即可看到效果

    这段代码会使“从URL”变成第一项,改变返回的数组的元素顺序,即可改变多媒体选项卡的顺序

    add_filter('media_upload_tabs', 'modify_media_tabs');
    function modify_media_tabs($tabs) {
     return array(
      'type_url' => __('From URL'),
        'type' => __('From Computer'),
      'gallery' => __('Gallery'),
      'library' => __('Media Library')
     );
    }

    设置默认选项卡

    前面提到有人喜欢直接通过URL插入图片,那么就让“从URL”变成默认选中的选项卡吧,将下面代码放到主题的functions.php中查看效果

    add_filter('_upload_iframe_src', 'change_default_media_tab');
    function change_default_media_tab($uri) {
     return $uri.'&tab=type_url';
    }
     

    要默认选中其它选项卡,只需要更改tab后面的值:

    从媒体库 – tab=library

    从相册 – tab=gallery

    从计算机 – tab=type

    删除某个选项卡

    将下面的代码放到主题的functions.php中

    add_filter('media_upload_tabs', 'remove_media_library_tab');
    function remove_media_library_tab($tabs) {
        unset($tabs['library']);
        //unset($tabs['type_url']); //删除从URL
        //unset($tabs['gallery']); //删除从相册
        //unset($tabs['type']); //删除从计算机
      
        return $tabs;
    }

    媒体库选项卡将被删除

    添加自定义消息

    add_action( 'post-upload-ui', 'media_upload_infobox' );
    add_action('pre-html-upload-ui','media_std_upload_infobox');
      
    function media_upload_infobox() {
    ?>
    <div style="background:#FFCC66; color:#000000; padding:10px; text-align:center">
    自定义消息
    </div>
      
    <?php
    }
      
    function media_std_upload_infobox() {
    ?>
    <div style="background:#FFCC66; color:#000000; padding:10px; text-align:center">
    使用标准上传工具上传界面的自定义消息
    </div>
      
    <?php
    }

    效果如下

    添加自定义消息

    WordPress 3.5.0以上版本的定制方法

    3.5以上media_upload_tabs这个钩子虽然存在但已经没有任何用处,3.5以上可以用media_view_strings这个filter来修改界面。下面的代码演示如何删除Create Gallery和Set Featured Image。

    add_filter('media_view_strings', 'remove_media_library_tabs');
    function remove_media_library_tabs( $strings ) {
    
        $strings["insertMediaTitle"] = "插入图片";
        $strings["insertIntoPost"] = "点击插入";
        $strings["addMedia"] = "添加图片";
        $strings["returnToLibrary"] = "回到已上传";
        
        $strings["mediaLibraryTitle"] = "已上传";
        
        //创建相册
        unset( $strings["createNewGallery"]);
        $strings["createGalleryTitle"] = "";
    
        //创建新的播放列表
        unset( $strings["createNewPlaylist"]);
        $strings["createPlaylistTitle"] = "";
        
        //创建音频播放列表
        unset( $strings["createNewPlaylist"] );
        $strings["createPlaylistTitle"] = "";
        
        //创建视频播放列表
        unset( $strings["createNewVideoPlaylist"] );
        $strings["createVideoPlaylistTitle"] = "";
        
        //特色图片
        unset( $strings["setFeaturedImageTitle"] );
    
        return $strings;
    }

    可以unset的字符串变量如下

    Array
    (
        [url] => URL
        [addMedia] => Add Media
        <a href="http://www.solagirl.net/?s="></a> => Search
        [select] => Select
        [cancel] => Cancel
        [selected] => %d selected
        [dragInfo] => Drag and drop to reorder images.
        [uploadFilesTitle] => Upload Files
        [uploadImagesTitle] => Upload Images
        [mediaLibraryTitle] => Media Library
        [insertMediaTitle] => Insert Media
        [createNewGallery] => Create a new gallery
        [returnToLibrary] => ← Return to library
        [allMediaItems] => All media items
        [noItemsFound] => No items found.
        [insertIntoPost] => Insert into post
        [uploadedToThisPost] => Uploaded to this post
        [warnDelete] => You are about to permanently delete this item.
      'Cancel' to stop, 'OK' to delete.
        [insertFromUrlTitle] => Insert from URL
        [setFeaturedImageTitle] => Set Featured Image
        [setFeaturedImage] => Set featured image
        [createGalleryTitle] => Create Gallery
        [editGalleryTitle] => Edit Gallery
        [cancelGalleryTitle] => ← Cancel Gallery
        [insertGallery] => Insert gallery
        [updateGallery] => Update gallery
        [addToGallery] => Add to gallery
        [addToGalleryTitle] => Add to Gallery
        [reverseOrder] => Reverse order
    )

    参考资料:remove other tabs in new wordpress media gallery

  • 相关阅读:
    Digital Video Stabilization and Rolling Shutter Correction using Gyroscope 论文笔记
    Distortion-Free Wide-Angle Portraits on Camera Phones 论文笔记
    Panorama Stitching on Mobile
    Natural Image Stitching with the Global Similarity Prior 论文笔记 (三)
    Natural Image Stitching with the Global Similarity Prior 论文笔记(二)
    Natural Image Stitching with the Global Similarity Prior 论文笔记(一)
    ADCensus Stereo Matching 笔记
    Efficient Large-Scale Stereo Matching论文解析
    Setting up caffe on Ubuntu
    Kubernetes配置Secret访问Harbor私有镜像仓库
  • 原文地址:https://www.cnblogs.com/huangcong/p/7647667.html
Copyright © 2011-2022 走看看