编程语言
首页 > 编程语言> > PHP-将Google Analytics(分析)添加到WordPress的next_post_link函数

PHP-将Google Analytics(分析)添加到WordPress的next_post_link函数

作者:互联网

致力于在自定义主题中设置Google Analytics(分析).我利用站点内的previous_post_link和next_post_link函数进行一些导航.现在,我尝试在其中添加一些Google Analytics(分析).

我已经将此添加到我的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;
}

当我尝试这样做时,我又遇到500错误.如果我替换ga(‘…’);经过测试的垃圾,它将加载并正常工作.

谁知道这是为什么,我该如何解决?

解决方法:

尝试转义报价.由于您在str_replace()上使用单引号,因此必须在函数本身中转义引号.

 $link = str_replace('" rel="next">', '" onclick="ga(\'send\', \'event\', \'NavNext\', \'click\');" rel="next">', $link);

标签:google-analytics,wordpress,php
来源: https://codeday.me/bug/20191118/2031170.html