make_clickable在Wordpress中不适用于冒号

ozxc1zmp  于 2023-01-20  发布在  WordPress
关注(0)|答案(1)|浏览(109)

我在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将转换为具有此URL的HTML链接:

https://www.google.com.ua/search?q=los+angeles+automotive+stores&rlz=1C1CHZL_ruUA745UA745&tbs=lrf

我该怎么做才能让它起作用?

3ks5zfa0

3ks5zfa01#

我发现自定义函数,解决了我的问题。我用下面的代码:

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="$0">$1</a>',
        '<a href="http://$0">$1</a>'
    );
    $text = preg_replace($patt, $repl, $text);
    echo $text;
}
add_filter( 'the_content', 'my_clickable_links', 12 );

相关问题