[英]Adding Google Analytics to Wordpress' next_post_link function
Working on setting up Google Analytics within a custom theme. 致力于在自定义主题中设置Google Analytics。 I make use of the previous_post_link and next_post_link functions within my site to do some navigation pieces.
我利用我网站中的previous_post_link和next_post_link函数来做一些导航。 Now I'm trying to add in some Google Analytics to those.
现在我正在尝试为这些添加一些Google Analytics。
I've added this to my functions.php file: 我已经将它添加到我的functions.php文件中:
add_filter('next_post_link', 'ga_next_post_link');
function ga_next_post_link($link) {
$link = str_replace('" rel="next">', '" onclick="ga('send', 'event', 'NavNext', 'click');" rel="next">', $link);
return $link;
}
add_filter('previous_post_link', 'ga_previous_post_link');
function ga_previous_post_link($link) {
$link = str_replace('" rel="last">', '" onclick="ga('send', 'event', 'NavLast', 'click');" rel="last">', $link);
return $link;
}
When I try that, I get a 500 error thrown back at me. 当我尝试这个时,我收到了500错误。 If I replace the
ga('...');
如果我更换
ga('...');
junk with test
, it will load and work fine. 垃圾与
test
,它将加载和工作正常。
Anyone know why this is, and how I can fix it? 任何人都知道这是为什么,以及如何解决它?
Try escaping your quotes. 尝试转义你的报价。 Since you're using single quotes on
str_replace()
you have to escape the quotes in the function itself. 由于您在
str_replace()
上使用单引号,因此必须转义函数本身的引号。
$link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.