Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to load a css and js file for use in WP admin only... WordPress

  • SOLVED

I just want to know the best way to load/include a css and js file in the wp admin when a certain theme is activated. These files will be exist inside the theme folder.

mytheme/library/css/admin.css
mytheme/library/scripts/admin.js

I'd like to call them both from my functions.php file.

Answers (1)

2010-11-28

Buzu B answers:

You need to use a conditional and check if you are in admin area:

if(is_admin()){
wp_enqueue_script();//params needed.
wp_enqueue_style();//params needed
}

Check the documentation for more info:

[[LINK href="http://codex.wordpress.org/Function_Reference/is_admin"]]http://codex.wordpress.org/Function_Reference/is_admin[[/LINK]]
[[LINK href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script"]]http://codex.wordpress.org/Function_Reference/wp_enqueue_script[[/LINK]]
[[LINK href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style"]]http://codex.wordpress.org/Function_Reference/wp_enqueue_style[[/LINK]]


Joe Calithia comments:

I was hoping for an exact solution using my file paths included in the question. Still a little confused after looking over the wp docs.


Buzu B comments:

I can do it for you and then explain how I did it.
I will send you a direct message with my contact information.


Buzu B comments:

Try this:
if(is_admin()){
$src1 = get_bloginfo('stylesheet_url') . '/library/css/admin.css';
$src2 = get_bloginfo('stylesheet_url') . '/library/scripts/admin.js';
wp_enqueue_script( 'myThemeScript', $src2, '', '', true );
wp_enqueue_style( 'myThemeStyle', $src1, '', '', 'screen' );
}


It should work just fine. In case it doesn't, look at the source code generated by the browser and check the paths being generated.
That code should go on your functions.php file.


Joe Calithia comments:

Thanks Buzu.

Actually the stylesheet_url needed to be template_url which seems to be working.