How To Include CSS and jQuery in my wordpress plugin?

How To Include CSS and jQuery in my wordpress plugin ?…………………………………….. 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 …

via Css Website development » Search Results » ajax:

How To Include CSS and jQuery in my wordpress plugin?

How To Include CSS and jQuery in my wordpress plugin ?
……………………………………..

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’));
……………………………………..

See http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Example
function my_init_method()
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ‘http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js’);

add_action(‘init’, ‘my_init_method’);
?>
……………………………………..

IMHO is following easier/saver even for mu-pugin users(not sure if it also works with ssl).
function your_css_and_js()
wp_register_style(‘your_css_and_js’, plugins_url(‘style.css’,__FILE__ ));
wp_enqueue_style(‘your_css_and_js’);
wp_register_script( ‘your_css_and_js’, plugins_url(‘your_script.js’,__FILE__ ));
wp_enqueue_script(‘your_css_and_js’);

add_action( ‘admin_init’,’your_css_and_js’);It took me aslo some time before I found the (for me) best solution which is foolproof imho.
Cheers
……………………………………..

To include CSS and jQuery in your plugin is easy, try this:
// register jquery and style on initialization
add_action(‘init’, ‘register_script’);
function register_script()
wp_register_script( ‘custom_jquery’, plugins_url(‘/js/custom-jquery.js’, __FILE__), array(‘jquery’), ’2.5.1′ );

wp_register_style( ‘new_style’, plugins_url(‘/css/new-style.css’, __FILE__), false, ’1.0.0′, ‘all’);

// use the registered jquery and style above
add_action(‘wp_enqueue_scripts’, ‘enqueue_style’);

function enqueue_style()
wp_enqueue_script(‘custom_jquery’);

wp_enqueue_style( ‘new_style’ );
I found this great snipped from this site How to include jQuery and CSS in WordPress – The WordPress Way
Hope that helps.

For more info: How To Include CSS and jQuery in my wordpress plugin?

Css Website development » Search Results » ajax

How To Include CSS and jQuery in my wordpress plugin?

Share this post:

Related Posts

Leave a Comment