WordPress 3.8 版本带来最大的变化就是全新的扁平化后台界面,所有的菜单图标和其他地方的图标都是使用字体图标,所以支持界面放大和缩小,并且能够适应各种配色方案,但是通过插件新增的菜单图标,如果使用图片的话,并不兼容,那么怎么办呢?
Dashicons 就是 3.8 带入的 WordPress 核心中的图标字体,现在 WordPress 后台所使用的图标就是 Dashicons,这些图标是基于矢量的,可以按照任意大小去缩放都不会失真。所以如果你觉得 dashicons 某个图标适合你的插件,可以直接使用,比如我的 WPJAM WordPress 插件 就是用 Dashicons 中的 cloud 图标:
add_menu_page( 'WPJAM', 'WPJAM', 'manage_options', 'wpjam-basic', 'wpjam_basic_setting_page', 'dashicons-performence');
其中第六个 $icon_url 参数指定为:dashicons-performance 就是设置 WPJAM Basic 插件为 dashicons 中的 performance 图标。
如果 Dashicons 中的图标还不能满足你的需求,那么你可以使用 SVG 图片,和图标字体一样,SVG 图片也是可以任意大小缩放不失真的。我们可以通过 Data URI scheme 方式插入,比如最新版的微信机器人图标这样设置后台菜单图标(效果如上图):
add_menu_page('微信机器人', '微信机器人', 'manage_options', 'weixin-robot', 'weixin_robot_basic_page', 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iNDAwIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCwgMCwgNDAwLCA0MDAiPgogIDxnIGlkPSJMYXllciAxIj4KICAgIDxwYXRoIGQ9Ik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OS4zNzkgMzA4LjI4MiwzMzkuNDg5IEwzMTYuODM5LDMzNy4xNzYgTDMzNC44NzksMzQ2Ljg5IEMzNDQuODI0LDM1Mi4zMjUgMzUzLjE1LDM1Ni4yNTcgMzUzLjM4MSwzNTUuNzk0IEMzNTMuNjEzLDM1NS4yMTYgMzUxLjY0NywzNDguMjc4IDM0OS4xMDMsMzQwLjI5OSBDMzQzLjMyMSwzMjIuNDkgMzQzLjIwNSwzMjMuNzYyIDM1MC45NTMsMzE4LjIxMiBDNDM4LjE0NCwyNTUuNjUxIDM2MS41OTIsMTMwLjA2OCAyNDguNjEzLDE1MC41MzYgeiBNMjQ2LjQxNiwyMDIuNDU3IEMyNTEuMjcyLDIwNS42OTUgMjUzLjgxNiwyMTMuNzkgMjUxLjczNSwyMTkuNjg4IEMyNDcuMzQxLDIzMi4yOTIgMjI4LjQ5MiwyMzMuMjE3IDIyMy40MDMsMjIxLjA3NSBDMjE3LjYyMSwyMDcuMDgzIDIzMy41OCwxOTQuMTMxIDI0Ni40MTYsMjAyLjQ1NyB6IE0zMjMuNjYyLDIwMy44NDUgQzMzMS4yOTQsMjExLjEzIDMzMC4wMjIsMjIzLjUwNCAzMjEuMTE4LDIyOC4xMjkgQzMwNy40NzMsMjM1LjA2NyAyOTMuMTM0LDIyMS4xOTEgMzAwLjE4OCwyMDcuODkyIEMzMDQuODEzLDE5OS4zMzUgMzE2LjcyNCwxOTcuMjU0IDMyMy42NjIsMjAzLjg0NSB6IE0yMjAuNDMsMzI4Ljc3MiIgZmlsbD0iI2ZmZiIvPgogIDwvZz4KICA8ZGVmcy8+Cjwvc3ZnPg==');
同样第六个 $icon_url 参数位置设置为一长串,就是把微信 SVG 图标转出 base64 字符串格式。具体做法是首先找到 Font屌这个中文图标字体中找到微信图标,然后使用这个工具将他转成 base64 的字符串格式,然后输入 data:image/svg+xml;base64,[data]([data]为 base64 字符串)即可。