1

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).

5
  • 1
    "the error log indicated wp_enqueue_style() evaluated to false." wp_enqueue_style() does not return anything. Logging its return value is not going to tell you anything about whether the style was enqueued. Commented Jul 13, 2018 at 12:13
  • That being said, both of your examples would work. Are you sure the issue isn't just with your CSS, and not whether or not the file has been enqueued? Make sure your rules are specific enough to override existing rules. Commented Jul 13, 2018 at 12:20
  • The 2nd one is exactly what I use without issue. Have you confirmed in your browser's inspector palette that it's 404ing? If not, Jacob's 2nd answer is probably it - perhaps your CSS needs to be more specific?
    – Peter HvD
    Commented Jul 13, 2018 at 13:29
  • I have checked Inspect Element--->Sources and my CSS file does not appear.@Peter I will check the 404 suggestion.
    – cag8f
    Commented Jul 13, 2018 at 16:29
  • @JacobPeattie OK I resolved my issue. It was indeed good ole user error. It was in-part related to my mistake of using get_stylesheet_directory_uri() in one part of my code, and get_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.
    – cag8f
    Commented Jul 14, 2018 at 4:35

1 Answer 1

0

Try this code by adding in function.php file.

function Newcustom_script_enqueue() {
  wp_enqueue_style( 'customstyle', get_template_directory_uri() . '/newtheme.css', array(),'1.0.0', 'all' );
}
add_action('wp_enqueue_scripts', 'Newcustom_script_enqueue');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.