Including CSS and JS on Admin Screen of Custom Theme Options

I am creating a custom theme with a theme options page.

I would like to style the options page and do not want to include inline styles. Is there any way to include an external stylesheet from say

TEMPLATEPATH . '/css/admin.css'

I have also found this chunk of code and it seems to work – link

function admin_register_head() {
 $siteurl = get_option('siteurl');
 $url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/yourstyle.css';
 echo "<link rel="stylesheet" type="text/css" href="https://wordpress.stackexchange.com/questions/2073/$url" />\n";
}
add_action('admin_head', 'admin_register_head');

What is the best way?

4 Answers
4

If you create an admin theme plugin from the Codex steps, you will notice it says not to insert stylesheets as per above – although the above will work.

If you place the following inside your admin theme file, it will serve the same purpose, but uses the wp_enqueue_styles approach:

function add_admin_theme_styles() {
    wp_register_style($handle="mytheme-theme-admin-styles", $src = plugins_url('wp-admin.css', __FILE__), $deps = array(), $ver="1.0.0", $media="all");
    wp_enqueue_style('mytheme-theme-admin-styles');}
    add_action('admin_print_styles', 'add_admin_theme_styles');

Leave a Comment