20

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');

}
32

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:

    <?php
      //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!

  • 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? – user1933824 Aug 20 '13 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 '13 at 4:16
  • 1
    Because I'm using a child theme I changed the path function to get_stylesheet_directory_uri() – Cazuma Nii Cavalcanti Jun 19 '17 at 15:21
0

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>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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