【问题标题】:How do I load my script AFTER ELEMENTOR's scripts at the end of the body如何在正文末尾的 ELEMENTOR 脚本之后加载我的脚本
【发布时间】:2020-10-27 21:39:50
【问题描述】:

我一直在试图弄清楚 Wordpress 中的 Elementor(和 Elementor Pro)插件正在做什么以使其脚本最后加载到正文中,因为我的插件脚本需要在这些之后加载。它需要在 end body 标记之前加载。

<script type='text/javascript' src='https://example.com/wp-content/plugins/elementor/assets/lib/swiper/swiper.jquery.min.js?ver=4.4.3'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var elementorFrontendConfig = {"isEditMode":"","is_rtl":"","breakpoints":{"xs":0,"sm":480,"md":768,"lg":1025,"xl":1440,"xxl":1600},"urls":{"assets":"https:\/\/example.com\/wp-content\/plugins\/elementor\/assets\/"},"settings":{"page":[],"general":{"elementor_global_image_lightbox":"yes","elementor_enable_lightbox_in_editor":"yes"}},"post":{"id":406,"title":"Chat","excerpt":""}};
/* ]]> */
</script>
<script type='text/javascript' src='https://example.com/wp-content/plugins/elementor/assets/js/frontend.min.js?ver=2.2.1'></script>
</body>
</html>

我已尝试提高我的入队脚本的优先级:

add_action( 'wp_enqueue_scripts', 'ajax_chat_enqueue_scripts',999999,1);

我在页脚的末尾加入了“footer=true”:

wp_enqueue_script( 'chat-post', plugins_url( '/chat-post.js', __FILE__ ), null, null, true );

但是在 frontend.min.js 脚本之后,我的脚本仍然不会显示。

我已经在 Google 上搜索过这个主题,但我最终得到了关于如何在页脚中获取脚本的相同答案。 Elementor 正在做些什么来迫使他们的脚本陷入困境。

我找到了一个解决方案:像这样在我的插件中加入我的脚本:

add_action( 'elementor/frontend/after_enqueue_scripts', 'ajax_chat_enqueue_scripts', 999999, 1);

但这似乎只是一个 hack。 我很想用'wp_enqueue_scripts'标签来解决它。

【问题讨论】:

    标签: javascript html wordpress elementor


    【解决方案1】:

    如果您的脚本依赖于其他一个或多个脚本并且必须在这些脚本之后插入才能正常工作,那么在 wordpress 中您可以使用 wp_enqueue_script() 并传递您的脚本的依赖数组(wp_enqueue_script 中的第三个参数称为 $deps)取决于,到这个方法。

    wp_enqueue_script( string $handle, string $src = '', array $deps = array(),
     string|bool|null $ver = false, bool $in_footer = false )
    

    示例:

    wp_register_script('myScript', get_template_directory_uri() . '/javascripts/myscript.js',
     array('jquery','bootstrap'), null, true );
    

    在上面的例子中,myscript 依赖于 jquery 和 bootstrap 脚本。

    或者出于任何原因,如果您想一个接一个地插入脚本,试试这个:

    function my_scripts() {
            wp_register_script('myscript', get_template_directory_uri() . '/javascripts/myscript.js', null, null, true );
            wp_enqueue_script('myscript');
    }
    add_action( 'wp_enqueue_scripts', 'my_scripts', 20, 1)
    

    播放和更改 wp_enqueue_scripts 钩子的 add_action 方法的 $priority 参数

    【讨论】:

    • 它不依赖于任何其他脚本,它只是与其他一些脚本冲突(它使用requirejs),因此需要放在最后。
    • 对不起,但就像我在问题中展示的那样,我已经尝试过使用优先级设置。它没有帮助。我什至把它设置为 9999999,因为在 Elementor 插件的某个地方我看到他们使用了 999999 ......但无论如何这也不起作用。顺便说一句,我还注意到他们代码中的某个地方,他们以某种方式取消注册 wp_enqueue_scripts 中的所有内容..所以就像他们在做一个技巧来强制他们的脚本最后排在队列中。
    猜你喜欢
    • 2020-07-28
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多