Dashicons 概述
什么是 Dashicons
Dashicons 是 WordPress 自带的一套矢量图标字体系统,专门为 WordPress 管理后台设计。这套图标系统自 WordPress 3.8 版本引入,现已成为 WordPress 核心的标准图标库。
主要特点
| 特性 |
说明 |
| 字体图标 |
基于字体而非图像,可无限缩放不失真 |
| 轻量级 |
单个字体文件包含所有图标,加载快速 |
| 易于使用 |
通过 CSS 类名即可调用 |
| 完全免费 |
开源授权,可自由使用 |
| 响应式 |
适配各种屏幕尺寸 |
| 高分辨率 |
Retina 显示屏上表现优秀 |
加载 Dashicons
自动加载情况
WordPress 会在以下场景自动加载 Dashicons:
– 所有后台管理页面
– 登录页面
– 前台(当管理工具栏显示时)
手动加载方法
// 在主题的 functions.php 中
function my_theme_enqueue_dashicons() {
wp_enqueue_style('dashicons');
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_dashicons');
使用 Dashicons
基本使用方法
通过 CSS 类名使用
<!-- 基本用法 -->
<span class="dashicons dashicons-admin-home"></span>
<!-- 不同尺寸 -->
<span class="dashicons dashicons-admin-home dashicons-16"></span>
<span class="dashicons dashicons-admin-home dashicons-32"></span>
<span class="dashicons dashicons-admin-home dashicons-64"></span>
<!-- 带颜色的图标 -->
<span class="dashicons dashicons-yes" style="color: #46b450;"></span>
在 WordPress 菜单中使用
// 为管理菜单添加图标
add_menu_page(
'我的页面',
'我的页面',
'manage_options',
'my-page-slug',
'my_page_callback',
'dashicons-admin-generic', // 图标类名
6
);
// 为子菜单添加图标
add_submenu_page(
'my-page-slug',
'子页面',
'子页面',
'manage_options',
'my-subpage-slug',
'my_subpage_callback'
);
// 子菜单图标需要额外设置
在按钮中使用
<button class="button">
<span class="dashicons dashicons-download"></span> 下载
</button>
<a href="#" class="button button-primary">
<span class="dashicons dashicons-update"></span> 更新
</a>
在表单中使用
<label>
<input type="checkbox" name="option">
<span class="dashicons dashicons-yes-alt"></span> 同意条款
</label>
<div class="form-field">
<span class="dashicons dashicons-email-alt"></span>
<input type="email" placeholder="邮箱地址">
</div>
Dashicons 图标分类与常用图标
管理菜单图标
| 图标 |
CSS 类名 |
Unicode |
描述 |
| 📊 |
dashicons-dashboard |
\f226 |
仪表盘 |
| 📝 |
dashicons-admin-post |
\f109 |
文章 |
| 🖼️ |
dashicons-admin-media |
\f104 |
媒体库 |
| 🔗 |
dashicons-admin-links |
\f103 |
链接 |
| 📄 |
dashicons-admin-page |
\f105 |
页面 |
| 💬 |
dashicons-admin-comments |
\f101 |
评论 |
| 🎨 |
dashicons-admin-appearance |
\f100 |
外观 |
| 🔌 |
dashicons-admin-plugins |
\f106 |
插件 |
| 👥 |
dashicons-admin-users |
\f110 |
用户 |
| 🛠️ |
dashicons-admin-tools |
\f107 |
工具 |
| ⚙️ |
dashicons-admin-settings |
\f108 |
设置 |
编辑功能图标
| 图标 |
CSS 类名 |
Unicode |
描述 |
| ✏️ |
dashicons-edit |
\f464 |
编辑 |
| 🗑️ |
dashicons-trash |
\f182 |
删除 |
| ➕ |
dashicons-plus |
\f132 |
添加 |
| ➖ |
dashicons-minus |
\f460 |
减少 |
| ✅ |
dashicons-yes |
\f147 |
是/确认 |
| ❌ |
dashicons-no |
\f158 |
否/取消 |
| 🔄 |
dashicons-update |
\f113 |
更新/刷新 |
| ⬆️ |
dashicons-arrow-up |
\f142 |
向上箭头 |
| ⬇️ |
dashicons-arrow-down |
\f140 |
向下箭头 |
| ⬅️ |
dashicons-arrow-left |
\f141 |
向左箭头 |
| ➡️ |
dashicons-arrow-right |
\f139 |
向右箭头 |
媒体与文件图标
| 图标 |
CSS 类名 |
Unicode |
描述 |
| 📷 |
dashicons-camera |
\f306 |
相机 |
| 🎥 |
dashicons-video-alt3 |
\f499 |
视频 |
| 🎵 |
dashicons-format-audio |
\f127 |
音频 |
| 📄 |
dashicons-media-default |
\f119 |
文件 |
| 📊 |
dashicons-chart-bar |
\f185 |
图表 |
| 📈 |
dashicons-chart-line |
\f238 |
折线图 |
社交媒体图标
| 图标 |
CSS 类名 |
Unicode |
描述 |
| 📱 |
dashicons-smartphone |
\f470 |
手机 |
| 🌐 |
dashicons-admin-site |
\f319 |
网站 |
| 📧 |
dashicons-email |
\f466 |
邮箱 |
| 📞 |
dashicons-phone |
\f525 |
电话 |
| 📍 |
dashicons-location |
\f230 |
位置 |
| 👤 |
dashicons-admin-users |
\f110 |
用户 |
| 🔒 |
dashicons-lock |
\f160 |
锁/安全 |
小工具与通知图标
| 图标 |
CSS 类名 |
Unicode |
描述 |
| 🔔 |
dashicons-bell |
\f117 |
通知/铃声 |
| ⭐ |
dashicons-star-filled |
\f155 |
星标(实心) |
| ⭐ |
dashicons-star-empty |
\f154 |
星标(空心) |
| ❤️ |
dashicons-heart |
\f487 |
心形 |
| 👍 |
dashicons-thumbs-up |
\f529 |
点赞 |
| 👎 |
dashicons-thumbs-down |
\f542 |
点踩 |
| 🏷️ |
dashicons-tag |
\f323 |
标签 |
| 📌 |
dashicons-pin |
\f464 |
图钉 |
| 🕒 |
dashicons-clock |
\f469 |
时钟 |
高级使用方法
自定义 CSS 样式
/* 自定义图标颜色和大小 */
.custom-icon {
color: #0073aa; /* WordPress 蓝色 */
font-size: 24px; /* 图标大小 */
vertical-align: middle; /* 垂直对齐 */
margin-right: 5px; /* 右边距 */
}
/* 旋转动画 */
.spinning-icon {
animation: dashicons-spin 2s infinite linear;
}
@keyframes dashicons-spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* 悬停效果 */
.hover-icon:hover {
color: #d54e21; /* 悬停颜色 */
transform: scale(1.2); /* 放大效果 */
transition: all 0.3s ease;
}
通过伪元素使用
/* 在标题前添加图标 */
.entry-title:before {
content: "\f109"; /* Unicode 编码 */
font-family: dashicons; /* 字体族 */
margin-right: 10px;
vertical-align: middle;
}
/* 在列表项前添加图标 */
.custom-list li:before {
content: "\f147"; /* 勾选图标 */
font-family: dashicons;
color: #46b450; /* 绿色 */
margin-right: 8px;
}
在短代码中使用
// 创建带图标的短代码
function icon_shortcode($atts) {
$atts = shortcode_atts(array(
'icon' => 'admin-home',
'size' => '24',
'color' => '#333',
'text' => ''
), $atts);
return sprintf(
'<span class="dashicons dashicons-%s" style="font-size:%spx;color:%s;"></span> %s',
esc_attr($atts['icon']),
esc_attr($atts['size']),
esc_attr($atts['color']),
esc_html($atts['text'])
);
}
add_shortcode('icon', 'icon_shortcode');
// 使用示例:[icon icon="email" size="20" color="#0073aa" text="联系我们"]
在自定义文章类型中使用
// 注册自定义文章类型时指定图标
register_post_type('product', array(
'labels' => array(
'name' => '产品',
'singular_name' => '产品'
),
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-products', // 使用 Dashicon
'supports' => array('title', 'editor', 'thumbnail')
));
最佳实践和技巧
性能优化建议
// 1. 只在需要时加载
function load_dashicons_on_demand() {
if (!is_admin()) {
// 只在特定页面加载
if (is_page('contact')) {
wp_enqueue_style('dashicons');
}
}
}
add_action('wp_enqueue_scripts', 'load_dashicons_on_demand');
// 2. 使用 CDN 版本(如果需要)
function use_dashicons_cdn() {
wp_deregister_style('dashicons');
wp_register_style('dashicons',
'https://cdnjs.cloudflare.com/ajax/libs/dashicons/1.0.0/dashicons.min.css');
}
可访问性考虑
<!-- 添加屏幕阅读器文本 -->
<span class="dashicons dashicons-download" aria-hidden="true"></span>
<span class="screen-reader-text">下载文件</span>
<!-- 或者使用 aria-label -->
<button aria-label="下载文件">
<span class="dashicons dashicons-download"></span>
</button>
响应式设计
/* 响应式图标大小 */
.responsive-icon {
font-size: 1.5em; /* 使用相对单位 */
}
@media (max-width: 768px) {
.responsive-icon {
font-size: 1.2em;
}
}
@media (max-width: 480px) {
.responsive-icon {
font-size: 1em;
}
}
常见问题解决
Dashicons 不显示的问题
// 问题:图标显示为方块或乱码
// 解决方案:
function fix_dashicons_issue() {
// 1. 确保正确加载 Dashicons
wp_enqueue_style('dashicons');
// 2. 检查 WordPress 版本
global $wp_version;
if (version_compare($wp_version, '3.8', '<')) {
// 旧版本需要手动添加
wp_enqueue_style('dashicons-legacy',
'https://cdnjs.cloudflare.com/ajax/libs/dashicons/1.0.0/dashicons.min.css');
}
}
add_action('wp_enqueue_scripts', 'fix_dashicons_issue', 999);
图标颜色问题
/* 确保图标颜色可继承 */
.icon-link {
color: inherit; /* 继承父元素颜色 */
}
/* 解决 Safari 上的颜色问题 */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.dashicons {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
}
与其他图标库冲突
// 如果与其他图标库(如 Font Awesome)冲突
function prevent_icon_conflicts() {
// 为 Dashicons 添加命名空间
wp_add_inline_style('dashicons', '
.dashicons {
font-family: dashicons !important;
}
');
}
add_action('wp_enqueue_scripts', 'prevent_icon_conflicts', 20);
资源和工具
官方资源
- 官方图标库: https://developer.wordpress.org/resource/dashicons/
- GitHub 仓库: https://github.com/WordPress/dashicons
- Unicode 参考: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/css/dashicons.css
实用工具
// 获取所有 Dashicons 的简单方法
jQuery(document).ready(function($) {
// 在控制台查看所有 Dashicon 类名
var dashicons = [];
$('<i>').addClass('dashicons').each(function() {
var classes = $(this).attr('class').split(' ');
classes.forEach(function(className) {
if (className.startsWith('dashicons-') && !dashicons.includes(className)) {
dashicons.push(className);
}
});
});
console.log('可用 Dashicons:', dashicons.sort());
});
浏览器书签工具
创建一个书签,点击即可显示所有 Dashicons:
javascript:(function(){
var style=document.createElement('style');
style.textContent='.dashicons-preview{display:inline-block;margin:5px;padding:10px;border:1px solid #ddd;border-radius:3px;text-align:center;width:100px;}';
document.head.appendChild(style);
var div=document.createElement('div');
div.innerHTML='<h2>Dashicons 预览</h2>';
document.body.appendChild(div);
var icons=['admin-site','dashboard','admin-post','admin-media','admin-links','admin-page','admin-comments','admin-appearance','admin-plugins','admin-users','admin-tools','admin-settings'];
icons.forEach(function(icon){
var preview=document.createElement('div');
preview.className='dashicons-preview';
preview.innerHTML='<span class="dashicons dashicons-'+icon+'"></span><br>'+icon;
div.appendChild(preview);
});
})()
总结
Dashicons 是 WordPress 生态系统中不可或缺的一部分,它为开发者和设计师提供了一套统一、美观、实用的图标系统。通过合理使用 Dashicons,可以:
- 保持一致性:与 WordPress 管理界面风格统一
- 提高开发效率:无需额外寻找和加载图标资源
- 优化性能:字体图标比图片图标加载更快
- 增强用户体验:清晰直观的视觉传达
无论是开发主题、插件,还是定制 WordPress 网站,掌握 Dashicons 的使用都能显著提升项目的专业性和用户体验。