Wordpress:自动更改帖子中的特定 URL

Gri*_*cha 6 php wordpress redirect

我找到了一个解决方案来更改我的 wordpress 主题中的链接,但不是内容中的链接。如何获取内容中的 URL,以便我也可以更改它们?

我需要使用the content过滤器。但是如何更改像 apple.com/test/apple.com/test-123/、apple.com、microsoft.com、microsoft.com/test/ 这样的 URL。该函数还应该正确更改内容中每个匹配的 URL。

add_filter('the_content ', 'function_name');
Run Code Online (Sandbox Code Playgroud)

不幸的是,类似问题答案不起作用。

这是我更改链接的工作解决方案,但不是内容中的链接。

add_filter('the_content ', 'function_name');
Run Code Online (Sandbox Code Playgroud)

小智 0

可以尝试使用类似the_content过滤器的东西来做到这一点:

add_filter('the_content', function($content){

  // filter $content and replace urls
  $content = str_replace('http://old-url', 'http://new-url', $content);

  return $content;
});
Run Code Online (Sandbox Code Playgroud)

更多: https: //developer.wordpress.org/reference/hooks/the_content/