黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素
原文:http://www.solagirl.net/customize-wordpress-media-upload-ui.html
WordPress编写文章界面的添加媒体按钮允许用户上传多媒体文件,但并不是每个人都能用的顺手,有的人倾向于引用外部图片,所以希望“从URL上传”这一项是默认选中的,有的人喜欢从媒体库中挑选图片,如果定制一下WordPress添加媒体界面,就能称心如意了。
注:该代码只适用于<3.5的版本,3.5以上定制方法请看文章最后一节。

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
黄聪:如何使用钩子定制WordPress添加媒体界面,去除不需要的元素的更多相关文章
- [转]WordPress“添加媒体”文件时只显示上传到当前文章的附件图片
使用WordPress的朋友应该都清楚,特别是喜欢图文并茂的网站,肯定离不开的就是WordPress文章编辑页面的“添加媒体”按钮,每次点击就能弹出一个插入多媒体的界面,然后页面默认就会列举加载所有最 ...
- 黄聪:主机宝安装wordpress注意事项
1.web环境安装PHP使用5.4.21-nts-03版本 2.web环境安装Mysql使用5.5.45版本 3.创建好站点后,给站点的public_html目录添加IIS_xxx用户最高权限,添加N ...
- 黄聪:定制化WordPress后台自定义仪表盘
WordPress作为一博客管理系统,相对来说已经相当简洁了,对用户也十分友好,新手也极易上手. 仪表盘是我们登陆WordPress后看到的后台界面,映入眼帘的是各种各样的信息,如WordPress ...
- 黄聪:《跟黄聪学WordPress插件开发》
续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- 黄聪:wordpress教程
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)
先看看CodeSmith的工作原理: 简单的说:CodeSmith首先会去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等,之后再根据用户自定义好的模板文件,用数据库结构中的关键字替代 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
随机推荐
- mysql 批量kill
select concat('kill ',id,';') t from information_schema.processlist order by t
- Oval框架如何校验枚举类型的一种思路
前言: Oval校验框架被广泛集成于各类接口参数校验中, 其方便的注解语法, 易读性和扩展性. 几乎成了java后端服务代码的标配. 有人会很疑惑, 都已经是枚举类型了, 还需要校验吗? 其实这边更确 ...
- linux cent os 6 的安装
目前,只有图片,没有仔细写,这是在虚拟机内的安装:
- C++学习(二十九)(C语言部分)之 顺序表
一.数据结构组织 存放数据的方式 精心选择的数据结构可以提升效率 数据结构 1.逻辑结构 一对多关系 父与子 一对一关系 排队中 多对多关系 两地的路线 2.存储结构 数据存放的位置关系 顺序存储数据 ...
- LeetCode - Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- 将koa+vue部署到服务器
很久很久以前,就对前后端如何分离,后端如何把代码部署到服务器有浓厚的兴趣,最近在阿里云上申请了一个服务器,试试水吧! 本文参考了文章<基于Node的Koa2项目从创建到打包到云服务器指南> ...
- sqlyog数据库管理软件下载
下载安装包 一路next 链接:https://pan.baidu.com/s/1tdIwtYEW11MNWk--Hqd3dw 提取码:0ffp 复制这段内容后打开百度网盘手机App,操作更方便哦 然 ...
- doubleclick video notes
1,vast duration it must math this format ,if use “00:00:7 ” it will tip “ ” <Duration>00:00:0 ...
- MySQL Transaction--两阶段提交事务
分布式事务两阶段提交 在分布式事务中,需要协调所有分布式原子事务参与者,并决定提交或回滚分布式事务,因此采用两阶段提交协议: 第一阶段为请求阶段或表决阶段,事务协调者通知事务参与者准备提交或取消事务, ...
- jenkins 使用smtp2http 邮件服务,扩展灵活的构建通知功能
smtp2http 是一个很方便的可以将smtp 转换为http 服务的工具,同时也支持扩展的开发,我们可以使用此工具 扩展灵活的ci.cd 生命周期管理,而不是简单的邮件处理 备注: 使用docke ...