Javascript Google Analytics事件跟踪代码在添加到functions.php时会破坏wordpress站点
我通过functions.php添加了一个全局按钮,该按钮使用以下代码显示在我的产品页面上:Javascript Google Analytics事件跟踪代码在添加到functions.php时会破坏wordpress站点,javascript,php,wordpress,google-analytics,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_book
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>';
}
函数在添加到启动按钮之后添加内容{
回声'
';
}
即使你用“双引号”打开echo,它也会中断,因为你要回显的代码中有双引号。我建议你使用一些带有php语法高亮显示的文本编辑器来查看它在哪里中断
试试我给你的密码,
关于你应该在你试图回显的代码中转义'这个词。你是指回显的开始和结束单引号还是Google Analytics参数?或者在GA javascript代码中使用双引号。因为你用单引号打开php回显代码,它会在回显块中的第一个单引号处终止(其余部分不是有效的PHP,因此会出现错误)。