| 开发者 | naiches |
|---|---|
| 更新时间 | 2026年9月23日 20:02 |
| PHP版本: | 7.4 及以上 |
| WordPress版本: | 7.1 |
| 版权: | GPLv2 or later |
| 版权网址: | 版权信息 |
dark-mode class to the admin body when active, and provides three filters for customization:
add_filter( 'dark_mode_dashboard_css', 'your_stylesheet_url' ) — replace the plugin's admin stylesheet with your own. This swaps the whole file rather than adding to it, so your stylesheet has to cover everything the plugin's does, and it does not affect the post editor's content area. To add a few rules of your own instead, enqueue a second stylesheet — see the FAQadd_filter( 'dark_mode_dashboard_default_preference', function() { return 'disabled'; } ) — change the default mode for new usersadd_filter( 'dark_mode_dashboard_editor_canvas', '__return_false' ) — disable dark mode for the editor content areaDon't edit the files inside the plugin — an update overwrites them. And dark_mode_dashboard_css is not the filter for this: it replaces the plugin's admin stylesheet rather than adding to it, and it does not reach the post editor's content area, which is a separate stylesheet.
To add rules of your own, enqueue your own stylesheet from your theme's functions.php:
add_action( 'admin_enqueue_scripts', function () {
wp_enqueue_style(
'my-admin-tweaks',
get_stylesheet_directory_uri() . '/admin-tweaks.css',
array( 'dark-mode-dashboard' ),
'1.0'
);
} );
Naming dark-mode-dashboard as a dependency makes your file load after the plugin's, so your rules win without needing !important. Note that a dependency also pulls the plugin's stylesheet in for users who have dark mode switched off, who would otherwise download almost nothing. If that matters to you, drop the dependency and hook in at a later priority instead:
add_action( 'admin_enqueue_scripts', function () {
wp_enqueue_style( 'my-admin-tweaks', get_stylesheet_directory_uri() . '/admin-tweaks.css', array(), '1.0' );
}, 20 );
The post content renders inside an iframe, and only styles enqueued on enqueue_block_assets are carried into it:
add_action( 'enqueue_block_assets', function () {
if ( ! is_admin() ) {
return;
}
wp_enqueue_style(
'my-editor-fonts',
get_stylesheet_directory_uri() . '/editor-fonts.css',
array(),
'1.0'
);
} );
One thing catches people out. Inside that iframe the body element is itself the .editor-styles-wrapper element — not its parent. So write the selector as a single compound with no space between the two classes:
body.editor-styles-wrapper p { font: 22px/1.5 Georgia, serif; }
Written as body.dark-mode .editor-styles-wrapper p, with a space, it looks for a wrapper nested inside the body, finds nothing, and silently does nothing. Use body.dark-mode.editor-styles-wrapper p to apply a rule in dark mode only, or leave .dark-mode out to apply it in both modes.
dark-mode on the admin body when dark mode is on, and dark-mode-auto when the preference is Auto. Auto deliberately does not also get dark-mode, so that it can follow the operating system.
dark_mode_dashboard_css replaces the admin stylesheet rather than adding to it, and never reaches the post editor. Corrected, plus a new FAQ on adding your own CSS. Thanks to Trace Meekul.wc-tabs pseudo-element fill); now darkdark_mode_dashboard_editor_canvas filter to disable editor canvas dark modedark_mode_dashboard_default_preference filter for developersdark-mode body class for third-party CSS hooks