我正在尝试向我的WordPress博客添加自定义功能,如果访问者的地理位置位于加拿大,则会在所有帖子文本和/或超链接中将“bigalspets.com”替换为“bigalspets.ca”。
我使用GeoIP Detection plugin获取GeoIP信息,使用Code Snippets插件添加仅在网站前端运行的自定义功能。
到目前为止我的功能是:
function replace_if_canada( $text ) {
$geoip = geoip_detect2_get_info_from_current_ip();
if ( $geoip->country->isoCode == 'CA' ) {
return str_replace('bigalspets.com', 'bigalspets.ca', $text);
}
}
function replace_if_canada_shortcodes( $output ) {
$geoip = geoip_detect2_get_info_from_current_ip();
if ( $geoip->country->isoCode == 'CA' ) {
echo str_replace('bigalspets.com', 'bigalspets.ca', $output);
}
}
add_filter('the_content', 'replace_if_canada', 999);
add_filter('do_shortcode_tag', 'replace_if_canada_shortcodes', 999);
这是一个WP帖子的网址,其功能应该有效: https://eheimsupport.com/blog/themed-tanks/items/canyon-island/
问题:我的代码中的'replace_if_canada_shortcodes'部分无法正常工作,即代码替换正常帖子内容中的文本/网址,但不会替换Image Map Pro短代码的输出!
如果将鼠标悬停在内容正文中图像地图上的一个小“i”图标上,您会看到产品网址。这些并没有从.com改为.ca ...
如何在所有帖子和页面上使用该功能应用于所有内容。请帮忙! :(