39

im having troubles getting my custom stylesheet work on WP-ADMIN area. plugins_url('style.css', __FILE__) ); do i have to create folder in my plugins named css or do i just copy my .css to the wp-admin/css directory?

i tried both it doesnt seem to work for me.

and what values should be replaced to __FILE__?

sorry im kinda new to these stuff.

/*ADDS STYLESHEET ON WP-ADMIN*/
add_action( 'admin_enqueue_scripts', 'safely_add_stylesheet_to_admin' );
    function safely_add_stylesheet_to_admin() {
        wp_enqueue_style( 'prefix-style', plugins_url('style.css', __FILE__) );
    }


/*ADDS MY CUSTOM NAVIGATION BAR ON WP-ADMIN*/
add_action('admin_head', 'custom_nav');
function custom_nav(){
    include('custom_nav.html');

}

3 Answers 3

59

According to WordPress Codex (here):

admin_enqueue_scripts is the first action hooked into the admin scripts actions.

Example

Loading a CSS or JS files for all admin area:

//from functions.php

//First solution : one file
//If you're using a child theme you could use:
// get_stylesheet_directory_uri() instead of get_template_directory_uri()
add_action( 'admin_enqueue_scripts', 'load_admin_style' );
function load_admin_style() {
    wp_register_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
    //OR
    wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
}

//Second solution : two or more files.
//If you're using a child theme you could use:
// get_stylesheet_directory_uri() instead of get_template_directory_uri()
add_action( 'admin_enqueue_scripts', 'load_admin_styles' );
function load_admin_styles() {
    wp_enqueue_style( 'admin_css_foo', get_template_directory_uri() . '/admin-style-foo.css', false, '1.0.0' );
    wp_enqueue_style( 'admin_css_bar', get_template_directory_uri() . '/admin-style-bar.css', false, '1.0.0' );
}

do i have to create folder in my plugins named css or do i just copy my .css to the wp-admin/css directory?

No, put your CSS file together with the other, in your theme directory, then specify the path with:

get_template_directory_uri() . '/PATH_TO_YOUR_FILE'

For ex my file name is admin-style.css and i put it in a folder named css my path will looks like:

get_template_directory_uri() . '/css/admin-style.css'

Hope it helps!

3
  • may i just ask, if i want to import 3 more stylesheets. do i just add (x3) of this part wp_register_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' ); or a separate function? Aug 20, 2013 at 15:50
  • 1
    wp_register_style immediately followed by the related wp_enqueue_style (as in 'First solution') is absolutely unncessary and does nothing more/different than just use wp_enqueue_style with all params passed to wp_register_style.
    – gmazzap
    Sep 2, 2013 at 4:16
  • 3
    Because I'm using a child theme I changed the path function to get_stylesheet_directory_uri() Jun 19, 2017 at 15:21
15

If you want to make CSS changes for the admin panel. paste the below code in functions.php of your child theme

add_action('admin_head', 'my_custom_fonts'); // admin_head is a hook my_custom_fonts is a function we are adding it to the hook

function my_custom_fonts() {
  echo '<style>
    #posts-filter table.posts{
        table-layout:auto;   
    }
  </style>';
}
1
  • 2
    Thanks for that quick fix!
    – NSukonny
    Jan 10, 2020 at 5:15
2

For those who want add CSS file in admin area from custom plugin (or plugin's folder):

if (is_admin()) {

Get src if file in folder: (..myPluginFolder/myfolder/css/my.css)

$myCssFileSrc = plugins_url( '/myfolder/css/my.css', __FILE__ );

Get src if file in root plugin folder: (..myPluginFolder/my.css)

$myCssFileSrc = plugins_url( 'my.css', __FILE__ );

Enqueue (activate) stylesheet file:

wp_enqueue_style( 'my-css', $myCssFileSrc );
} //endif;

All manipulation in main plugin file (myplugin.php)

3
  • That's the same approach OP had in the question though didn't it, which mustn't have been working for them?
    – Rup
    Oct 25, 2020 at 12:38
  • 1
    The approach used in the question is not correct. I have presented a completely, personally tested, and workable code for connecting a CSS file to the admin part of the site. This is exactly how it is recommended to do it in the documentation for creating plugins: developer.wordpress.org/plugins/plugin-basics/… Oct 25, 2020 at 16:29
  • 2
    I can confirm the code @tagformat.com provided is workable and is the wordpress codex recommended method provided it is wrapped in a function and you and hook that function to your target enqueue action. Otherwise the css (or if using scripts) the js file is enqueued too soon.
    – BillieM
    Jan 23 at 21:58

Your Answer

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

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