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?
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:
When registering a page for the admin area, using
add_submenu_page
or any of the otheradd_{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:
Various hooks are then available for that page specifically, here are just a few possible actions for the now registered page.
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.You should not typically need to use
admin_head
,admin_print_scripts
oradmin_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..
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).Yea I am answering my own question … but I did get it to work.
Add this action to your theme’s functions.php file