Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果主机不是开发站点,则在wp_标题上显示谷歌分析_Php_Wordpress_Google Analytics - Fatal编程技术网

Php 如果主机不是开发站点,则在wp_标题上显示谷歌分析

Php 如果主机不是开发站点,则在wp_标题上显示谷歌分析,php,wordpress,google-analytics,Php,Wordpress,Google Analytics,下面代码的想法是,仅当网站不是开发网站时,才显示用于分析的google代码-因此我将其放在Wordpress下的我的孩子主题中,但有些东西不起作用 知道我的错在哪里吗 add_action('wp_header', 'add_googleanalytics'); function add_googleanalytics() { if ($_SERVER['HTTP_HOST']==="mywebsite.com" || $_SERVER['HTTP_HOST']==="www.mywe

下面代码的想法是,仅当网站不是开发网站时,才显示用于分析的google代码-因此我将其放在Wordpress下的我的孩子主题中,但有些东西不起作用

知道我的错在哪里吗

add_action('wp_header', 'add_googleanalytics');

function add_googleanalytics()
{
    if ($_SERVER['HTTP_HOST']==="mywebsite.com" || $_SERVER['HTTP_HOST']==="www.mywebsite.com") 
    {
        if (@$_COOKIE["COOKIENAME"] !== "COOKIEVALUE")
        {?>
     <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

          ga('create', 'GOOGLE ID', 'auto');
          ga('send', 'pageview');
         </script>
    <?php
    }
    }
}
add_action('wp_header','add_googleanalytics');
函数add_googleanalytics()
{
如果($\u服务器['HTTP\u主机]]==“mywebsite.com”|$\u服务器['HTTP\u主机]]==“www.mywebsite.com”)
{
如果(@$_COOKIE[“COOKIENAME”]!=“COOKIEVALUE”)
{?>
(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口、文档、“脚本”和https://www.google-analytics.com/analytics.js","ga",;
ga(“创建”、“谷歌ID”、“自动”);
ga(‘发送’、‘页面浏览’);

WordPress中用于区分其运行环境的推荐方法是将
WP\u DEBUG
设置为布尔值


可以在根目录中的
wp config.php
文件中定义此选项,代码如下:

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', false);
在ActionHook中,您可以定义一个条件语句;该语句仅在GA脚本处于生产模式时加载

if(WP_DEBUG === false) {
    // Google Analytics goes here.
}

为什么不将它过滤到GA上呢?有各种可能的原因:但是,让我们从更多关于什么不起作用的信息开始,钩子开机了吗?你是在运行子主题吗?考虑使用WP函数GGySITEYURURL()。甚至可以使用Google Tag Manager来实现带有URL触发规则的GA,在您的更新中,在GA上对其进行过滤更容易。只需将您的IP添加为过滤。不要让它变得更难。我只需编辑我的问题以添加一些重要注释…但将尝试您的解决方案。
wp config.php
文件在按co时被忽略des从本地开发到服务器。Live server可能具有存储在
wp config.php
中的特定配置,本地环境可能会发现这些配置不可用。@BaliAngeloCogerino如果您觉得解决方案有用,可以将此答案标记为已接受。