Javascript CDN脚本未在WordPress中排队

Javascript CDN脚本未在WordPress中排队,javascript,php,wordpress,bootstrap-4,cdn,Javascript,Php,Wordpress,Bootstrap 4,Cdn,我试图将CDN中的脚本和样式放入Wordpress中,以便在代码的页脚中使用此JSFIDLE: 我可以看到6个脚本正在加载到我的页面源代码中,但是我试图嵌入到页脚中的JSFIDLE不起作用 我猜一些新脚本与原始脚本冲突,或者我需要在footer.php文件中将它们排队吗 所以atm,my functions.php有以下代码: <?php function load_stylesheets() { wp_register_style('bootstrap', get_template_

我试图将CDN中的脚本和样式放入Wordpress中,以便在代码的页脚中使用此JSFIDLE:

我可以看到6个脚本正在加载到我的页面源代码中,但是我试图嵌入到页脚中的JSFIDLE不起作用

我猜一些新脚本与原始脚本冲突,或者我需要在footer.php文件中将它们排队吗

所以atm,my functions.php有以下代码:

<?php

function load_stylesheets()
{
wp_register_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css',
    array(), false, 'all');
wp_enqueue_style('bootstrap');


    wp_register_style('style', get_template_directory_uri() . '/style.css',
    array(), false, 'all');
wp_enqueue_style('style');



}

add_action('wp_enqueue_scripts', 'load_stylesheets'); 




function include_jquery()
{
wp_deregister_script('jquery');

wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.1.3.min.js', '', 1, true);



add_action('wp_enqueue_scripts', 'jquery');
}

add_action('wp_enqueue_scripts', 'include_jquery');



function loadjs()
{
wp_register_script('customjs', get_template_directory_uri() . '/js/scripts.js', '', 1, true);
wp_enqueue_script('customjs');

}

add_action('wp_enqueue_scripts', 'loadjs');



// Next 6 styles are new (I can see them when i view page source, but I think they are clashing with something, as the JSfiddle I'm trying to input isnt working.

wp_register_style( 'Bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
wp_enqueue_style('Bootstrap');


wp_register_style( 'Bootstrap_Theme', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css' );
wp_enqueue_style('Bootstrap_Theme');


wp_register_style( 'Bootstrap_Slider', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.8.0/css/bootstrap-slider.min.css' );
wp_enqueue_style('Bootstrap_Slider');


wp_register_script( 'jQuery', 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', null, null, true );
wp_enqueue_script('jQuery');


wp_register_script( 'Bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', null, null, true );
wp_enqueue_script('Bootstrap');

wp_register_script( 'Bootstrap_Slider', 'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.8.0/bootstrap-slider.min.js', null, null, true );
wp_enqueue_script('Bootstrap_Slider');

//

这里有多个错误,您应该将脚本的使用限制在除“管理”面板之外的所有地方。您没有检查CDN是否真的在工作,并且您没有退路
get\u template\u directory\u uri()
不应用于加载
style.css
文件,而应使用
get\u stylesheet\u uri()
。您正在使用多个函数,无需任何理由和加载。您对
wp\u register\u style
wp\u register\u style
的调用不在任何功能范围内

我将只粘贴我正在使用的加载和排队脚本和样式,请随意复制!希望这会对你有所帮助,如果答案有帮助,别忘了竖起大拇指

<?php add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
  if ( ! is_admin() ) {

    /**
    * Register then enqueue bootstrap bundle js (Bootstrap 5.x required)
    *
    * Check if CDN's url is valid, if not return fallback
    */
    $test_bootstrap_bundle_js = @fopen( 'https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.bundle.min.js', 'r' );
    if ( $test_bootstrap_bundle_js !== false ) {
      wp_register_script( 'bootstrap_bundle_js', '//stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.bundle.min.js' );
    } else {
      wp_register_script( 'bootstrap_bundle_js', get_template_directory_uri() . '/assets/js/bootstrap.bundle.min.js' );
    };
    wp_enqueue_script( 'bootstrap_bundle_js' );


    /**
    * Register then enqueue bootstrap css (Bootstrap 5.x required)
    *
    * Check if CDN's url is valid, if not return fallback
    */
    $test_bootstrap_css = @fopen( 'https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css', 'r' );
    if ( $test_bootstrap_css !== false ) {
      wp_register_style( 'bootstrap_css', '//stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css' );
    } else {
      wp_register_style( 'bootstrap_css', get_stylesheet_uri() . '/assets/css/bootstrap.min.css' );
    };
    wp_enqueue_style( 'bootstrap_css' );

    /**
    * Register then enqueue style css
    */
    wp_register_style( 'style_css', get_stylesheet_uri(), array( 'bootstrap_css' ) );
    wp_enqueue_style( 'style_css' );

  };
}; ?>

wp\u register\u脚本中,我们在
数组('bootstrap\u bundle\u js','jquery\u js')
之后指定一个脚本数组来加载新脚本。它使用在注册样式/script
wp\u register\u script('script\u js',…
这里
'script\u js'
是句柄。

对于您的上一条评论,我们可以执行以下操作:

对于您的JS脚本:

(1)
jquery.min.js
例如:
wp\u enqueue\u脚本('jquery\u js','//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js');

(2)
bootstrap.min.js
依赖于(1), 例如:
wp_enqueue_脚本('bootstrap_js','//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js',数组('jquery_js');

(3)
bootstrap slider.min.js
依赖于(1)和(2), 例如:
wp_enqueue_脚本('bootstrap_slider_js','//cdnjs.cloudflare.com/ajax/libs/bootstrap slider/9.8.0/bootstrap slider.min.js',数组('jquery_js','bootstrap_js');

(4)
script.js
依赖于(1)和(2)和(3), 例如:
wp\u排队脚本('script\u js',get\u template\u directory\u uri()。/assets/js/script.js',数组('jquery\u js','bootstrap\u js','bootstrap\u slider\u js');

对于您的CSS脚本:

(1)
bootstrap.min.css
例如:(此处相同,但使用了
wp\u排队\u样式

(2)
bootstrap slider.css
依赖于(1)


(3)
style.css
依赖于(1)和(2),

为什么最后6个脚本不在那些“wp\u enqueue\u scripts”函数的范围内?除此之外,您应该尝试在句柄前面加上类似“thats me”的前缀。如果你没有任何奇怪的缓存/缓存设置,你应该在源代码中看到这些脚本。嘿,迭戈,我在源代码中尝试了它们,但范围滑块仍然无法工作,它确实调整了字体大小+超链接颜色。所以我猜这些新的引导脚本和样式+我尝试加载的jquery(使范围滑块工作)与原来的有冲突吗?嗯,检查每个访问网站的请求的URL不是太过分了吗?就像这样,如果事情进展缓慢,你会浪费很多时间等待这些消息。只有我的2个百分点。这是最佳实践的一部分。浪费这么多时间吗?这一个很有趣,尝试一下,告诉我你是否浪费了那么多时间e、 您可以在这里了解有关@fopen的更多信息。我们几乎正在打开请求的文件(我们会这样做),如果可以读取(几乎是真的还是假的)然后我们加载它,否则我们会退后。我只是说,如果必须这样做,我会将它缓存一段时间,我不认为有必要继续检查每个请求,如果url仍然存在。或者至少,这种方法真的很悲观:P无意冒犯任何人。问题只是在任何cas中以及在最后分享科学今天,你是开发人员,这是你的项目!给了你一个简短的方法示例,请参阅新答案
<?php
    /**
    * Register then enqueue script js
    */
    wp_register_script( 'script_js', get_template_directory_uri() . '/assets/js/script.js', array( 'bootstrap_bundle_js', 'jquery_js' ) );
    wp_enqueue_script( 'script_js' ); ?>