I’m trying to load a CSS file for my WordPress post area, but having no luck. I have read over the wp_enqueue_style function and came up with the code below, but it doesn’t load up. Is there a tag or character missing from my code. I have a custom write panel when a user post that I want to style with the CSS file. Any help would be great.
Here is what I have in my themes functions.php
file:
function mytheme_add_init() {
$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("functions", $file_dir."/scripts/custom.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/scripts/custom.js", false, "1.0");
}
Just hook your callback into
admin_print_styles
, i.e.:Alternately, you could add an
is_admin()
conditional wrapper inside your callback, and hook intowp_enqueue_scripts
:But the absolute best approach is to hook into your Theme’s admin page, via
admin_print_styles-appearance_page_{pagename}
:This is a custom hook specifically for your appearance page, as defined via your
add_theme_page()
call.Late answer: As both previous answers showed old, incomplete or complicated ways, here’s an updated version that works the v3.5+ way.
What’s different?
Here’s the list
admin_enqueue_scripts
hook. This hookwp_enqueue_style()
s last argument is the targeted media and it’s already set toall
per default. No need to add it.get_template_directory_uri()
function to retrieve the URl for our stylesheet. No need to check the option value fortemplate_directory
here.get_template_directory()
to retrieve the path and wrap it into afilemtime()
call to get the last time where the stylesheet was edited. This way we append a new version number as query argument and force browser to reload the stylesheet if there’s a new version. No need to force hard reloads with Ctrl + F5.wp-admin.css
,ie
(even worse) or the color scheme. The really tough part is to check the color scheme, as this file carries most of what is styled in the admin UI and is a user setting. We want to add this as dependency as well.admin_head-*
hook, where*
is the pageslug. We hook it two times to take new as well as edited posts into consideration.Here’s the code for your
functions.php
file.Alternatives?
In case you just want to add styles to the TinyMCE WYSIWYG editor, you can use
add_editor_style()
to register your themes stylesheet in the admin areas text editor as well. The path you add as argument is relative to your themes root. In yourfunctions.php
file:It’s as simple as that.
Here is a quick way to add some style in admin head. hope this helps: