Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  198] [ 1]  / answers: 1 / hits: 148305  / 14 Years ago, tue, september 21, 2010, 12:00:00

How To Include CSS and jQuery in my WordPress plugin ?


More From » jquery

 Answers
18

For styles wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );



Then use: wp_enqueue_style('namespace'); wherever you want the css to load.



Scripts are as above but the quicker way for loading jquery is just to use enqueue loaded in an init for the page you want it to load on: wp_enqueue_script('jquery');



Unless of course you want to use the google repository for jquery.



You can also conditionally load the jquery library that your script is dependent on:



wp_enqueue_script('namespaceformyscript', 'http://locationofscript.com/myscript.js', array('jquery'));



Update Sept. 2017



I wrote this answer a while ago. I should clarify that the best place to enqueue your scripts and styles is within the wp_enqueue_scripts hook. So for example:



add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');
function callback_for_setting_up_scripts() {
wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
wp_enqueue_style( 'namespace' );
wp_enqueue_script( 'namespaceformyscript', 'http://locationofscript.com/myscript.js', array( 'jquery' ) );
}


The wp_enqueue_scripts action will set things up for the frontend. You can use the admin_enqueue_scripts action for the backend (anywhere within wp-admin) and the login_enqueue_scripts action for the login page.


[#95552] Saturday, September 18, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dawnc

Total Points: 612
Total Questions: 94
Total Answers: 98

Location: Sweden
Member since Fri, Apr 16, 2021
3 Years ago
;