简体   繁体   English

wordpress 中的特定 Wp Enqueue 脚本

[英]Particular Wp Enqueue script in wordpress

When looking for a plugin, I ended up across this website在寻找插件时,我最终浏览了这个网站

I saw that there is a nice cloud animation .我看到有一个很好的云动画

So I started a search on google to find the source of the code and I found it here ... Preview于是我开始在google上搜索代码的来源,我在这里找到...预览

/JS /JS

<script type="text/javascript" src="js/ThreeWebGL.js"></script>
<script type="text/javascript" src="js/ThreeExtras.js"></script>
<script type="text/javascript" src="js/Detector.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>

/Html /HTML

<script id="vs" type="x-shader/x-vertex">
    varying vec2 vUv;
    void main() {
        vUv = uv;
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
</script>
<script id="fs" type="x-shader/x-fragment">
    uniform sampler2D map;
    uniform vec3 fogColor;
    uniform float fogNear;
    uniform float fogFar;
    varying vec2 vUv;
    void main() {
        float depth = gl_FragCoord.z / gl_FragCoord.w;
        float fogFactor = smoothstep( fogNear, fogFar, depth );
        gl_FragColor = texture2D( map, vUv );
        gl_FragColor.w *= pow( gl_FragCoord.z, 20.0 );
        gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );
    }
</script>
<script type="text/javascript">
    if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
    // Bg gradient
    var canvas = document.createElement( 'canvas' );
    canvas.width = 32;
    canvas.height = window.innerHeight;
    var context = canvas.getContext( '2d' );
    var gradient = context.createLinearGradient( 0, 0, 0, canvas.height );
    gradient.addColorStop(0, "#1e4877");
    gradient.addColorStop(0.5, "#4584b4");
    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height);
    document.body.style.background = 'url(' + canvas.toDataURL('image/png') + ')';
    // Clouds
    var container;
    var camera, scene, renderer, sky, mesh, geometry, material,
    i, h, color, colors = [], sprite, size, x, y, z;
    var mouseX = 0, mouseY = 0;
    var start_time = new Date().getTime();
    var windowHalfX = window.innerWidth / 2;
    var windowHalfY = window.innerHeight / 2;
    init();
    animate();
    function init() {
        container = document.createElement( 'div' );
        document.body.appendChild( container );
        camera = new THREE.Camera( 30, window.innerWidth / window.innerHeight, 1, 3000 );
        camera.position.z = 6000;
        scene = new THREE.Scene();
        geometry = new THREE.Geometry();
        var texture = THREE.ImageUtils.loadTexture( 'cloud10.png' );
        texture.magFilter = THREE.LinearMipMapLinearFilter;
        texture.minFilter = THREE.LinearMipMapLinearFilter;
        var fog = new THREE.Fog( 0x4584b4, - 100, 3000 );
        material = new THREE.MeshShaderMaterial( {
            uniforms: {
                "map": { type: "t", value:2, texture: texture },
                "fogColor" : { type: "c", value: fog.color },
                "fogNear" : { type: "f", value: fog.near },
                "fogFar" : { type: "f", value: fog.far },
            },
            vertexShader: document.getElementById( 'vs' ).textContent,
            fragmentShader: document.getElementById( 'fs' ).textContent,
            depthTest: false
        } );
        var plane = new THREE.Mesh( new THREE.Plane( 64, 64 ) );
        for ( i = 0; i < 8000; i++ ) {
            plane.position.x = Math.random() * 1000 - 500;
            plane.position.y = - Math.random() * Math.random() * 200 - 15;
            plane.position.z = i;
            plane.rotation.z = Math.random() * Math.PI;
            plane.scale.x = plane.scale.y = Math.random() * Math.random() * 1.5 + 0.5;
            GeometryUtils.merge( geometry, plane );
        }
        mesh = new THREE.Mesh( geometry, material );
        scene.addObject( mesh );
        mesh = new THREE.Mesh( geometry, material );
        mesh.position.z = - 8000;
        scene.addObject( mesh );
        renderer = new THREE.WebGLRenderer( { antialias: false } );
        renderer.setSize( window.innerWidth, window.innerHeight );
        container.appendChild( renderer.domElement );
        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
        window.addEventListener( 'resize', onWindowResize, false );
    }
    function onDocumentMouseMove( event ) {
        mouseX = ( event.clientX - windowHalfX ) * 0.25;
        mouseY = ( event.clientY - windowHalfY ) * 0.15;
    }
    function onWindowResize( event ) {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize( window.innerWidth, window.innerHeight );
    }
    function animate() {
        requestAnimationFrame( animate );
        render();
    }
    function render() {
        position = ( ( new Date().getTime() - start_time ) * 0.03 ) % 8000;
        camera.position.x += ( mouseX - camera.target.position.x ) * 0.01;
        camera.position.y += ( - mouseY - camera.target.position.y ) * 0.01;
        camera.position.z = - position + 8000;
        camera.target.position.x = camera.position.x;
        camera.target.position.y = camera.position.y;
        camera.target.position.z = camera.position.z - 1000;
        renderer.render( scene, camera );
    }
</script>

My question now :我现在的问题:

I would like to set it up on my localhost site to test it on elementor.我想在我的本地主机站点上设置它以在 elementor 上测试它。

I will call the script with an html widget.我将使用 html 小部件调用脚本。

The problem I know there is an wp_enqueue_script option in functions.php and the script goes there ..我知道functions.php中有一个wp_enqueue_script选项的问题,脚本去那里..

but I can't understand the exact procedure.但我无法理解确切的程序。 I failed for hours to set up the scripts ...失败几个小时来安装脚本...

Can you show me how to integrate it ?你能告诉我如何集成它吗? I need a good example how use these scripts.我需要一个很好的例子来说明如何使用这些脚本。 to understand once and for all how to integrate scripts in wordpress child themes.一劳永逸地了解如何在 wordpress 子主题中集成脚本。

My setup : A clean install of wordpress with an empty hello theme and it's child installed .我的设置:全新安装的 wordpress 带有空的hello 主题,并且它是子安装的

Update更新

let's develop the subject a bit I did these manipulations in functions.php让我们稍微发展一下主题我在functions.php中做了这些操作

function my_scripts_method() {
    wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/ThreeWebGL.js',array( 'jquery' ));
    wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/ThreeExtras.js',array( 'jquery' ));
    wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/Detector.js',array( 'jquery' ));
    wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/RequestAnimationFrame.js',array( 'jquery' ));

}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

It seems to work.. the scripts are loaded I hope.它似乎有效......我希望脚本已加载。

I just have a message in the chrome console :我只是在 chrome 控制台中有一条消息:

Uncaught ReferenceError: Detector is not defined at (index): 117未捕获的 ReferenceError:检测器未在(索引)处定义:117

I guess because i haven't put html yet?我猜是因为我还没有把 html 呢?

what do you think of a snippet ?你觉得一个片段怎么样?

I use it to create and add_shortcode我用它来创建和 add_shortcode

something like that.. do you think it's safe ?类似的东西..你认为它安全吗?

    add_shortcode( 'amazing clouds', function () {

<script id="vs" type="x-shader/x-vertex">
    varying vec2 vUv;
    void main() {
        vUv = uv;
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
    }
</script>
<script id="fs" type="x-shader/x-fragment">
    uniform sampler2D map;
    uniform vec3 fogColor;
    uniform float fogNear;
    uniform float fogFar;
    varying vec2 vUv;
    void main() {
        float depth = gl_FragCoord.z / gl_FragCoord.w;
        float fogFactor = smoothstep( fogNear, fogFar, depth );
        gl_FragColor = texture2D( map, vUv );
        gl_FragColor.w *= pow( gl_FragCoord.z, 20.0 );
        gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );
    }
</script>
<script type="text/javascript">
    if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
    // Bg gradient
    var canvas = document.createElement( 'canvas' );
    canvas.width = 32;
    canvas.height = window.innerHeight;
    var context = canvas.getContext( '2d' );
    var gradient = context.createLinearGradient( 0, 0, 0, canvas.height );
    gradient.addColorStop(0, "#1e4877");
    gradient.addColorStop(0.5, "#4584b4");
    context.fillStyle = gradient;
    context.fillRect(0, 0, canvas.width, canvas.height);
    document.body.style.background = 'url(' + canvas.toDataURL('image/png') + ')';
    // Clouds
    var container;
    var camera, scene, renderer, sky, mesh, geometry, material,
    i, h, color, colors = [], sprite, size, x, y, z;
    var mouseX = 0, mouseY = 0;
    var start_time = new Date().getTime();
    var windowHalfX = window.innerWidth / 2;
    var windowHalfY = window.innerHeight / 2;
    init();
    animate();
    function init() {
        container = document.createElement( 'div' );
        document.body.appendChild( container );
        camera = new THREE.Camera( 30, window.innerWidth / window.innerHeight, 1, 3000 );
        camera.position.z = 6000;
        scene = new THREE.Scene();
        geometry = new THREE.Geometry();
        var texture = THREE.ImageUtils.loadTexture( 'cloud10.png' );
        texture.magFilter = THREE.LinearMipMapLinearFilter;
        texture.minFilter = THREE.LinearMipMapLinearFilter;
        var fog = new THREE.Fog( 0x4584b4, - 100, 3000 );
        material = new THREE.MeshShaderMaterial( {
            uniforms: {
                "map": { type: "t", value:2, texture: texture },
                "fogColor" : { type: "c", value: fog.color },
                "fogNear" : { type: "f", value: fog.near },
                "fogFar" : { type: "f", value: fog.far },
            },
            vertexShader: document.getElementById( 'vs' ).textContent,
            fragmentShader: document.getElementById( 'fs' ).textContent,
            depthTest: false
        } );
        var plane = new THREE.Mesh( new THREE.Plane( 64, 64 ) );
        for ( i = 0; i < 8000; i++ ) {
            plane.position.x = Math.random() * 1000 - 500;
            plane.position.y = - Math.random() * Math.random() * 200 - 15;
            plane.position.z = i;
            plane.rotation.z = Math.random() * Math.PI;
            plane.scale.x = plane.scale.y = Math.random() * Math.random() * 1.5 + 0.5;
            GeometryUtils.merge( geometry, plane );
        }
        mesh = new THREE.Mesh( geometry, material );
        scene.addObject( mesh );
        mesh = new THREE.Mesh( geometry, material );
        mesh.position.z = - 8000;
        scene.addObject( mesh );
        renderer = new THREE.WebGLRenderer( { antialias: false } );
        renderer.setSize( window.innerWidth, window.innerHeight );
        container.appendChild( renderer.domElement );
        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
        window.addEventListener( 'resize', onWindowResize, false );
    }
    function onDocumentMouseMove( event ) {
        mouseX = ( event.clientX - windowHalfX ) * 0.25;
        mouseY = ( event.clientY - windowHalfY ) * 0.15;
    }
    function onWindowResize( event ) {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize( window.innerWidth, window.innerHeight );
    }
    function animate() {
        requestAnimationFrame( animate );
        render();
    }
    function render() {
        position = ( ( new Date().getTime() - start_time ) * 0.03 ) % 8000;
        camera.position.x += ( mouseX - camera.target.position.x ) * 0.01;
        camera.position.y += ( - mouseY - camera.target.position.y ) * 0.01;
        camera.position.z = - position + 8000;
        camera.target.position.x = camera.position.x;
        camera.target.position.y = camera.position.y;
        camera.target.position.z = camera.position.z - 1000;
        renderer.render( scene, camera );
    }
</script>
} );

Things got very complicated very quickly.事情很快变得非常复杂。

I hope someone will come and show us how to integrate it into a wordpress page我希望有人来向我们展示如何将其集成到 wordpress 页面中

(with elementor is bonus) (与元素是奖金)

The HTML widget will strip out php tags for security reasons.出于安全原因,HTML 小部件将去掉 php 标签。 But you have several options which will allow you to achieve your goal.但是您有多种选择可以让您实现目标。 Here are three:这里有三个:

  1. Edit your child theme's functions.php file to allow php in the widget. 编辑您的子主题的functions.php 文件以允许在小部件中使用php。 (Bad idea) (馊主意)

  2. Write your own custom widget which enqueues the script you need.编写您自己的自定义小部件,将您需要的脚本排入队列。 (Better idea) (更好的主意)

  3. Don't use a widget at all.根本不要使用小部件。 Just add the script to the pages you want it using the Advanced Custom Fields plugin.只需使用高级自定义字段插件将脚本添加到您想要的页面即可。 (Best idea IMHO) (最好的主意恕我直言)

And since you asked about loading JS, here's a good resource for you.既然你询问了加载 JS,这里有一个很好的资源
And here's another on the topic of wp_enqueue_scripts() .这是另一个关于wp_enqueue_scripts()的话题。


Edit: step by step instructions for ACF plugin:编辑:ACF 插件的分步说明:

  1. Install Advanced Custom Fields plugin安装高级自定义字段插件
  2. Go to the Custom Fields settings and click Add New转到自定义字段设置,然后单击添加新
  3. Name the field group something like “Javascript Settings”将字段组命名为“Javascript 设置”
  4. Create rules to restrict where the fields will appear and who will see them创建规则来限制字段的显示位置以及谁将看到它们
  5. Next to Style, choose Standard (WP metabox)在样式旁边,选择标准(WP metabox)
  6. Click + Add Field单击 + 添加字段
  7. Name it “Header Script”将其命名为“标题脚本”
  8. Change Field Type to “Text Area”将字段类型更改为“文本区域”
  9. Change Formatting to “Convert HTML to tags”将格式更改为“将 HTML 转换为标签”
  10. Repeat from step 6, but this time call it “Footer Script”从第 6 步开始重复,但这次将其称为“页脚脚本”
  11. Publish the Field Group发布字段组
  12. In header.php, right before the closing </head> tag, add: <?php the_field('header_script'); ?>在 header.php 中,就在结束</head>标记之前,添加: <?php the_field('header_script'); ?> <?php the_field('header_script'); ?>
  13. In footer.php, right before the closing </body> tag, add: <?php the_field('footer_script'); ?>在footer.php 中,就在结束</body>标记之前,添加: <?php the_field('footer_script'); ?> <?php the_field('footer_script'); ?>
  14. Place your javascript files in a folder on your server (preferably in a child theme).将您的 javascript 文件放在服务器上的文件夹中(最好在子主题中)。
  15. Link your javascript in the new fields on your page using regular html <script> tags使用常规 html <script>标签将您的 javascript 链接到页面上的新字段中

Note: copy your header.php and footer.php files into your child theme and make the edits (steps 12 & 13) there to avoid losing these changes if your theme is updated.注意:将您的header.phpfooter.php文件复制到您的子主题中,并在那里进行编辑(步骤 12 和 13)以避免在您的主题更新时丢失这些更改。

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

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