将外部js文件添加到ID为的页脚

时间:2017-11-21 作者:Shiv Singh

我知道如何添加自定义/外部js,但我不知道如何传递jsid 而且

我将其用作:

wp_register_script( \'validation\', \'https://www.asd.in/script.php?id=3ff00a469474bbe71a9218a7f0377518\', array( \'jquery\' ) );

wp_enqueue_script( \'validation\' );
但我需要在页脚中输出,如:

<script type="text/javascript" id="3ff00a469474bbe71a9218a7f0377518" src="https://www.asd.in/script.php?id=3ff00a469474bbe71a9218a7f0377518"></script>
注:ID3ff00a469474bbe71a9218a7f0377518 most显示在输出中

1 个回复
SO网友:Shibi

您可以使用script_loader_tag

function enqueue_validation_script() {
    wp_register_script( \'validation\', \'https://www.asd.in/script.php?id=3ff00a469474bbe71a9218a7f0377518\', array( \'jquery\' ) );
    wp_enqueue_script( \'validation\' );
}
add_action(\'wp_enqueue_scripts\', \'enqueue_validation_script\');

function add_id_to_validation_script( $tag, $handle, $src ) {
    if ( \'validation\' === $handle ) {
        $tag = \'<script type="text/javascript" id="3ff00a469474bbe71a9218a7f0377518" src="\'.$src.\'"></script>\';
    }
    return $tag;
}
add_filter( \'script_loader_tag\', \'add_id_to_validation_script\', 10, 3 );

结束

相关推荐

为什么我的WordPress要加载旧版本的JQuery?

嗨,我正在尝试将引导javascript文件排队。但这取决于JQuery和Popper。js。因此,我从CDN加载PopperJ,我提到JQuery和PopperJ都是引导程序队列的依赖项。我并没有将JQuery排入队列,因为如果您知道Wordpress将加载它,只要您将其作为依赖项提及即可。如下所示:-wp_enqueue_script( \'popper\', \'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.mi