How to add external libraries in plugin or wordpress template

by Dani Arlandis — on


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=""></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' );

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’)