wordpress enqueue script code example
Example 1: enqueue wordpress
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
Example 2: wordpress enqueue script jquery dependency
function script_that_requires_jquery() {
wp_register_script( 'script-with-dependency', 'http://www.example.com/script-with-dependency.js', array( 'jquery' ), '1.0.0', true );
wp_enqueue_script( 'script-with-dependency' );
}
add_action( 'wp_enqueue_scripts', 'script_that_requires_jquery' );
Example 3: wordpress enqueue script
function add_theme_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_style( 'slider', get_template_directory_uri() . '/css/slider.css', array(), '1.1', 'all');
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
Example 4: wp_enqueue_script
function custom_theme_script(){
wp_register_style('bootstrap', get_stylesheet_directory_uri() . '/css/bootstrap.css' );
wp_enqueue_style('bootstrap');
wp_register_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('style');
wp_register_script('bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '20120206', true );
wp_enqueue_script('bootstrap.min');
}
add_action('wp_enqueue_scripts', 'custom_theme_script');