I have created a simple CSS file and I would like to apply it to one of my admin pages--specifically the 'edit post' page. I have tried two methods (described below), but both are failing--the CSS file in-question doesn't seem to load at all. My CSS file is located in my parent theme directory, named admin-style.css, and consists of:
.ea-hide-admin {
visibility: hidden;
}
Attempt 1: From the WordPress Codex page on enqueuing admin scripts. To my functions.php I added:
function load_custom_wp_admin_style() {
wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css');
wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
This failed, and the error log indicated wp_enqueue_style()
evaluated to false. I also tried the admin_head
hook here instead, but that failed with the same error.
Attempt 2: From this previous Stack Exchange question. To my functions.php I added:
function wpse239532_load_admin_style() {
wp_enqueue_style( 'admin_css', get_stylesheet_directory_uri() . '/admin-style.css', false, '1.0.0' );
}
add_action( 'admin_enqueue_scripts', 'wpse239532_load_admin_style' );
This also failed, and the error log indicated that wp_enqueue_style()
is evaluating to false.
I have confirmed the CSS file in-question exists in the proper directory (screenshot).
wp_enqueue_style()
does not return anything. Logging its return value is not going to tell you anything about whether the style was enqueued.get_stylesheet_directory_uri()
in one part of my code, andget_stylesheet_directory_uri()
in another part. Once I sorted that out, the second method worked as expected. So we can consider this resolved. How should we mark this question though? Also, yes, I see that wp_enqueue_style() doesn't return anything. I was originally returning wp_register_style(), and then naively assumed wp_enqueue_style() returned something similarly.