简体   繁体   English

wordpress 如何加载 javascript cdn

[英]wordpress how to load javascript cdn

hey i am new to wordpress and i am trying to load a javascript cdn into the website.嘿,我是 wordpress 的新手,我正在尝试将 javascript cdn 加载到网站中。

the following is the pixi js cdn i want to input to the wordpress website.以下是我要输入到 wordpress 网站的 pixi js cdn。

<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/6.2.0/cjs/pixi.min.js" integrity="sha512-4h3/xeKfvJ4wudsiZXc5EGdlC6WFEshvLv+e1qL6DdRyGaavPozaysXtneFj+gMsUg1uhp7/HyLu6o4yhAax6w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

may i enquire do i need to use a plugin to put in javascript code or is there any other way?请问我需要使用插件来输入 javascript 代码还是有其他方法?

Create a child theme for the divi theme.为 divi 主题创建一个主题。 Then you can add the below code to your child theme functions.php file然后您可以将以下代码添加到您的子主题函数中。php 文件

function load_scripts($hook) {
   wp_register_script( 'PixiJs', 'https://cdnjs.cloudflare.com/ajax/libs/pixi.js/6.2.0/cjs/pixi.min.js', null, null, true );
    wp_enqueue_script('PixiJs');
}
add_action('wp_enqueue_scripts', 'load_scripts');


function manage_script_attributes( $tag, $handle ){
    if ( $handle == 'PixiJs' ) {
        return str_replace( '<script', '<script integrity="sha512-4h3/xeKfvJ4wudsiZXc5EGdlC6WFEshvLv+e1qL6DdRyGaavPozaysXtneFj+gMsUg1uhp7/HyLu6o4yhAax6w==" crossorigin="anonymous" referrerpolicy="no-referrer"', $tag );
    }
    return $tag;    
}
add_filter('script_loader_tag','manage_script_attributes', 10, 2);

Read more about wp_register_script and wp_enqueue_script阅读更多关于wp_register_scriptwp_enqueue_script

Inorder to add attributes in the CDN to your script, make use of script_loader_tag as shown above.为了将 CDN 中的属性添加到您的脚本,请使用script_loader_tag ,如上所示。

Or if you are not much familiar with the above process, then you could install a code snippet plugin like Code Snippets and add the above code或者如果你对上面的过程不是很熟悉,那么你可以安装一个代码片段插件,比如Code Snippets并添加上面的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM