最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp enqueue script - How to en-queue bootstrap 4 to theme?

programmeradmin4浏览0评论

i tried enqueue the bootstap with functions php

wp_enqueue_style('bootstrap4', '.1.1/css/bootstrap.min.css');
wp_enqueue_script( 'boot1','.3.1.slim.min.js');
wp_enqueue_script( 'boot2','.js/1.14.3/umd/popper.min.js');
wp_enqueue_script( 'boot2','.1.1/js/bootstrap.min.js');

css seems to work but none of the js files are working..not sure what i am doing wrong..

please help thank

i tried enqueue the bootstap with functions php

wp_enqueue_style('bootstrap4', 'https://stackpath.bootstrapcdn/bootstrap/4.1.1/css/bootstrap.min.css');
wp_enqueue_script( 'boot1','https://code.jquery/jquery-3.3.1.slim.min.js');
wp_enqueue_script( 'boot2','https://cdnjs.cloudflare/ajax/libs/popper.js/1.14.3/umd/popper.min.js');
wp_enqueue_script( 'boot2','https://stackpath.bootstrapcdn/bootstrap/4.1.1/js/bootstrap.min.js');

css seems to work but none of the js files are working..not sure what i am doing wrong..

please help thank

Share Improve this question asked Jun 16, 2018 at 17:07 user145078user145078 2
  • Is your theme using wp_head()? – Nathan Johnson Commented Jun 16, 2018 at 17:25
  • yes i am using & confirmed that its having that ..problem seems like : wp_enqueue_script( 'boot2','stackpath.bootstrapcdn/bootstrap/4.1.1/js/bootstrap.min.js', array( 'jquery' ),'',true ); is not loading ..others are loading in header – user145078 Commented Jun 16, 2018 at 17:45
Add a comment  | 

2 Answers 2

Reset to default 7

You can use action hook and enqueue script and style to the site.

function my_scripts() {
    wp_enqueue_style('bootstrap4', 'https://stackpath.bootstrapcdn/bootstrap/4.1.1/css/bootstrap.min.css');
    wp_enqueue_script( 'boot1','https://code.jquery/jquery-3.3.1.slim.min.js', array( 'jquery' ),'',true );
    wp_enqueue_script( 'boot2','https://cdnjs.cloudflare/ajax/libs/popper.js/1.14.3/umd/popper.min.js', array( 'jquery' ),'',true );
    wp_enqueue_script( 'boot3','https://stackpath.bootstrapcdn/bootstrap/4.1.1/js/bootstrap.min.js', array( 'jquery' ),'',true );
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

If you want to add the scripts after jQuery then you can use the jQuery array for dependent and last argument true will include the JS at footer, if you want the js in header, you can remove that.

Try this:

function abc_load_my_scripts() {
  wp_enqueue_script( 'boot1','https://code.jquery/jquery-3.3.1.slim.min.js', array('jquery'));
  wp_enqueue_script( 'boot2','https://cdnjs.cloudflare/ajax/libs/popper.js/1.14.3/umd/popper.min.js', array('jquery'));
  wp_enqueue_script( 'boot3','https://stackpath.bootstrapcdn/bootstrap/4.1.1/js/bootstrap.min.js', array('jquery'));
}
add_action( 'wp_enqueue_scripts', 'abc_load_my_scripts', 999);

With the last argument array('jquery') you're telling WP to include the script AFTER jQuery. Note that every script must use a different handle (first parameter).

More info here.

When you check the <head>, are you certain the scripts aren't loaded?

Regards, Bjorn

发布评论

评论列表(0)

  1. 暂无评论