将脚本添加到wp_footer()函数

问题描述 投票:-1回答:1

有人可以指导我如何添加和调用wp_footer()函数中的所有脚本?

javascript jquery wordpress
1个回答
0
投票

将所有脚本添加到页脚是不好的选择。

为什么?因为在体内有许多插件,主题可能会使用一些脚本,这些脚本有望使用标题中的一些js库。如果您将在该代码之后执行所有脚本,那么您将获得“红色”控制台,其中包含大量无法运行的代码。

如果您的目标是首先更快地渲染您的网站,您可以使用插件,或者在测试后转到代码并删除/替换一些脚本。

但是如果你需要它,这里的代码是删除从头部执行的脚本并移动所有连接到头部的脚本

function remove_head_scripts() {
    remove_action('wp_head', 'wp_print_scripts');
    remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_head', 'wp_enqueue_scripts', 1);

    add_action('wp_footer', 'wp_print_scripts', 5);
    add_action('wp_footer', 'wp_enqueue_scripts', 5);
    add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts' );

此代码不会迁移脚本,这些脚本使用echoprint_r执行或仅包装到网站的html中。

© www.soinside.com 2019 - 2024. All rights reserved.