How to add external libraries in plugin or wordpress template

by Dani Arlandis — on

cover-image

WordPress includes a number of features to provide us with the addition of styles and javascript libraries to our site.

Bad practice

echo ‘<script src="<a href="http://code.jquery.com/jquery-1.9.1.js">http://code.jquery.com/jquery-1.9.1.js</a>"></script>’;

echo ‘<link href="’. plugins_url( 'css/mystyle.css' , __FILE__ ) . '" rel="stylesheet" type="text/css" />’;

Best practice

function my_scripts() {
wp_enqueue_script( 'jquery' );
wp_register_style( 'prefix-style', plugins_url('mystyle.css', __FILE__) );
wp_enqueue_style( 'prefix-style' );
}
add_action('wp_enqueue_scripts','my_scripts');

WordPress comes preinstalled with other popular jQuery libraries for add them only add wp_enqueue_script ('library name') into the fuction or widget

Ej. jQuery: wp_enqueue_script (‘jquery’)

Referencias:

http://codex.wordpress.org/Function_Reference/wp_enqueue_script http://codex.wordpress.org/Function_Reference/add_action http://codex.wordpress.org/Function_Reference/wp_enqueue_style http://codex.wordpress.org/Function_Reference/wp_register_style