make_clickable 不适用于 Wordpress 中的冒号
make_clickable doesn't work with colons in Wordpress
我在functions.php
中使用make_clickable
add_filter( 'the_content', 'make_clickable', 12 );
但它不适用于带冒号的 URL。它只在冒号之前制作可点击的文本。示例:
https://www.google.com.ua/search?q=los+angeles+automotive+stores&rlz=1C1CHZL_ruUA745UA745&tbs=lrf:!2m1!1e2!2m1!1e3!3sIAE,lf:1&tbm=lcl&ei=eZXnWZi1AuLA6ATJ9ojICg&start=0&sa=N&rllag=34020955,-118343579,8541&rlha=0&biw=1745&bih=885#rlfi=hd:;si:130388325671661058;mv:!1m3!1d118510.4446802296!2d-118.30250324999999!3d34.0113229!2m3!1f0!2f0!3f0!3m2!1i429!2i430!4f13.1
上面的 URL 将转换为 HTML link URL:
https://www.google.com.ua/search?q=los+angeles+automotive+stores&rlz=1C1CHZL_ruUA745UA745&tbs=lrf
我需要做什么才能让它发挥作用?
我找到了解决我问题的自定义函数。我使用了以下代码:
function my_clickable_links( $text ) {
$patt = array(
'%\b(?<!href=[\'"])(?>https?://|www\.)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*[\p{L}\p{N}]{2,})(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?)?(?![^<]*+</a>)%u',
'%\b(?<!http://)(?<!https://)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*(?:ru|com|net))(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?|\b)(?![^<]*+</a>)%u'
);
$repl = array(
'<a href="[=10=]"></a>',
'<a href="http://[=10=]"></a>'
);
$text = preg_replace($patt, $repl, $text);
echo $text;
}
add_filter( 'the_content', 'my_clickable_links', 12 );
我在functions.php
中使用make_clickableadd_filter( 'the_content', 'make_clickable', 12 );
但它不适用于带冒号的 URL。它只在冒号之前制作可点击的文本。示例:
https://www.google.com.ua/search?q=los+angeles+automotive+stores&rlz=1C1CHZL_ruUA745UA745&tbs=lrf:!2m1!1e2!2m1!1e3!3sIAE,lf:1&tbm=lcl&ei=eZXnWZi1AuLA6ATJ9ojICg&start=0&sa=N&rllag=34020955,-118343579,8541&rlha=0&biw=1745&bih=885#rlfi=hd:;si:130388325671661058;mv:!1m3!1d118510.4446802296!2d-118.30250324999999!3d34.0113229!2m3!1f0!2f0!3f0!3m2!1i429!2i430!4f13.1
上面的 URL 将转换为 HTML link URL:
https://www.google.com.ua/search?q=los+angeles+automotive+stores&rlz=1C1CHZL_ruUA745UA745&tbs=lrf
我需要做什么才能让它发挥作用?
我找到了解决我问题的自定义函数。我使用了以下代码:
function my_clickable_links( $text ) {
$patt = array(
'%\b(?<!href=[\'"])(?>https?://|www\.)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*[\p{L}\p{N}]{2,})(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?)?(?![^<]*+</a>)%u',
'%\b(?<!http://)(?<!https://)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*(?:ru|com|net))(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?|\b)(?![^<]*+</a>)%u'
);
$repl = array(
'<a href="[=10=]"></a>',
'<a href="http://[=10=]"></a>'
);
$text = preg_replace($patt, $repl, $text);
echo $text;
}
add_filter( 'the_content', 'my_clickable_links', 12 );