How can I copy the default styling for the admin and tell wordpress to use custom style instead of the default? Basically I want to change the colour and font without having to use !important syntax to force Wordpress to use it.
3 Answers
As noted by @Fusion, this has changed beginning with version 5. In order to override admin styles for 5.*, you need a hook in functions.php
, something like this:
function custom_admin() {
$url = get_settings('siteurl');
$url = $url . '/wp-content/themes/my-theme/wp-admin.css';
echo '<link rel="stylesheet" type="text/css" href="' . $url . '" />';
}
add_action('admin_head', 'custom_admin')
Sources:
Wordpress Codex
isitwp Snippet
Please note the the isitwp Snippet erroneously mixes html entitites with angle brackets. You should use angle brackets not entitieis.
-
Please use
get_option()
instead ofget_settings()
as that has been deprecated since 2.1.1! In fact, the "modern" best practice would be$url = get_stylesheet_directory_uri() . '/wp-admin.css';
Feb 6, 2022 at 21:08
Enqueue custom admin CSS file (custom-admin.css) in your WordPress backend using below function:
add_action('admin_print_styles', 'custom_admin_css');
add_action('wp_enqueue_scripts', 'custom_admin_css');
function custom_admin_css() {
if (is_admin()) {
wp_enqueue_style("custom-admin-css", get_bloginfo('template_directory'). "/custom_admin.css", false, false, "all");
}
}
Hope this works..!!
-
In current 5.* version of Wordpress it "works", but if you want to add custom CSS of Wordpres's admins interface (for example a purple button), you will not move anywhere. Wordpress loads all the native admin-css as the last element of the page - meaning, that all your custom CSS will be overriden.– FusionMar 21, 2019 at 11:32
In WordPress 5.x you can use the following hooks to add CSS to either the admin pages or the post edit page.
enqueue_block_editor_assets
to add your own styles to the Gutenberg page
admin_enqueue_scripts
to add your own styles to the admin pages
You can add the hooks in your functions.php
theme file.
-
1According to the [docs][1]
admin_print_styles
should not be used to enqueue styles to the admin page. Useadmin_enqueue_scripts
instead (both for scripts and styles). [1]: developer.wordpress.org/reference/hooks/admin_print_styles– RaukauteApr 4, 2021 at 19:20