javascript - 添加到 functions.php 时,Google Analytics 事件跟踪代码会破坏 wordpress 站点

标签 javascript php wordpress google-analytics

我使用以下代码通过 functions.php 添加了一个显示在我的产品页面上的全局按钮:

function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com" 
title="share using Line" rel="nofollow" 
class="wp_social_bookmarking_light_a"><img src="sample-image.png" 
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}

将以下 Google Analytics 事件跟踪代码添加到上述代码后,网站就会中断。

function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com" 
title="share using Line" rel="nofollow" 
class="wp_social_bookmarking_light_a" onClick="ga('send', 'event', 'social', 'line-button-click', 'line-button');"><img src="sample-image.png" 
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}

我已经通过将跟踪代码直接放在页面中成功地测试了它

<a href="website.com" onClick="ga('send', 'event', 'social', 'line-button-
click', 'line-button');">anchor text</a>       

我做错了什么?

最佳答案

如果你用单引号打开代码并在你想要回显的代码中使用它们,你需要转义它,像这样:

function add_content_after_addtocart_button_func() {
echo '
<div class="wsbl_line"><a href="http://line.me/R/msg/text/website.com" 
title="share using Line" rel="nofollow" 
class="wp_social_bookmarking_light_a" onClick="ga(\'send\', \'event\', 
\'social\', \'line-button-click\', \'line-button\');"><img src="sample-
image.png" 
width="135" height="30" class="wp_social_bookmarking_light_img"></a></div>';
}

即使你用 "双引号打开回显,它也会中断,因为你正在回显的代码中有双引号。我建议你使用一些带有 php 语法高亮的文本编辑器来查看它在哪里中断。

试试我给你的代码, 问候

关于javascript - 添加到 functions.php 时,Google Analytics 事件跟踪代码会破坏 wordpress 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47303176/

相关文章:

javascript - 这个 javascript 使我的浏览器崩溃

javascript - 如何从javascript中的去抖动函数返回值?

javascript window.open 函数

php - 在数据库中搜索整个字符串/短语

r - 通过 knit2wp() 将 Markdown 脚本上传到 WordPress 时的 latex 方程

php - 在 WooCommerce 中先显示当前类别,然后显示子类别

javascript - pubnub历史API调用总是返回timetoken

javascript - 填充选择菜单 onChange

php - 在 codeigniter 中获取列值数组

mysql - WordPress 网站加载缓慢