4

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='$url' />\n";
}
add_action('admin_head', 'admin_register_head');

What is the best way?

4 Answers 4

3

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

When registering a page for the admin area, using add_submenu_page or any of the other add_{TYPE}_page functions the fourth parameter accepts a unique identifier, this identifier designates the hook your registered page will use..

If i were to register an options for example:

add_options_page( 'Example Plugin Options', 'Example Plugin', 'manage_options', 'example-plugin-identifier', 'example_plugin_options' );

Various hooks are then available for that page specifically, here are just a few possible actions for the now registered page.

// load-{HANDLE}
add_action( 'load-example-plugin-identifier', 'example_plugin_callback' );
// admin_head-{HANDLE}
add_action( 'admin_head-example-plugin-identifier', 'example_plugin_callback' );
// admin_print_scripts-{HANDLE}
add_action( 'admin_print_scripts-example-plugin-identifier', 'example_plugin_callback' );
// admin_print_styles-{HANDLE}
add_action( 'admin_print_styles-example-plugin-identifier', 'example_plugin_callback' );

function example_plugin_callback() {

    // Run your code here

}

There is also the admin_enqueue_scripts hook, which provides the name of the current handle in the string/variable it passes along to callback functions.

add_action( 'admin_enqueue_scripts', 'example_plugin_callback' );

function example_plugin_callback( $handle ) {

    // If the handle is not the page registered earlier, return
    if( 'example-plugin-identifier' =! $handle )
        return;

    // Run your code here

}

You should not typically need to use admin_head,admin_print_scripts or admin_print_styles unless you have a specific requirement to target every administration page or perform conditional logic inside the callback to target specific registered pages.

Hope that helps..

3
  • Ah OK, this makes sense. Are you able to update the Codex page about creating an admin theme with this? codex.wordpress.org/Creating_Admin_Themes
    – davemac
    Commented Nov 10, 2010 at 0:27
  • I certainly could, but i'd have to sit down and build an admin theme before i go telling others how they should be doing it, time permitting i'll see if i can revise that codex entry..
    – t31os
    Commented Nov 10, 2010 at 10:59
  • Great! @abrudtkuhl, you should probably select this comment as the correct answer rather than mine I would think.
    – davemac
    Commented Nov 11, 2010 at 1:50
3

WP provides queues for scripts and styles. It allows to version URLs, automatically load dependencies, etc.

See wp enqueue style() in Codex for how to properly register your style and load only where you need it (at your custom page and not all of admin area).

1

Yea I am answering my own question ... but I did get it to work.

Add this action to your theme's functions.php file

function admin_register_head() {
    $url = get_bloginfo('template_directory') . '/css/admin.css';
    echo "<link rel='stylesheet' type='text/css' href='$url' />\n";
}
add_action('admin_head', 'admin_register_head');
1
  • 1
    If this solution works for you, you should click the "Accept" checkmark so others know you're no longer looking for it. But also check out the other answers, they offer good reasons why you should not write <link> tags yourself.
    – Jan Fabry
    Commented Nov 9, 2010 at 14:37

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.