如何在 WordPress edit.php 屏幕上添加 google 标签管理器脚本?

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

在这里,我尝试在 WordPress 后端 edit.php 屏幕上添加谷歌标签管理器脚本,但每当我添加脚本时,它都会在控制台中出现错误。

Refused to load the script 'https://www.googletagmanager.com/gtag/js?id=[TAG-ID]&ver=1.0.0' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' 'unsafe-eval'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

这是相同的代码

$func_page = array("edit","post","edit-tags");
if(in_array($current_page, $func_page, true)) {
    wp_enqueue_script(
       'google-analytics',
       'https://www.googletagmanager.com/gtag/js?id=[TAG-ID]',
       array(),
       '1.0.0'
    );
}

请帮助我如何在我的自定义插件中解决这个问题

php wordpress plugins google-analytics
1个回答
0
投票

您会想要使用

admin_enqueue_scripts

大致如下:

function add_gtag_to_admin() {
    wp_enqueue_script(
        'google-analytics',
        'https://www.googletagmanager.com/gtag/js?id=[TAG-ID]',
        array(),
        '1.0.0'
    );
}
add_action( 'admin_enqueue_scripts', 'add_gtag_to_admin' );
© www.soinside.com 2019 - 2024. All rights reserved.