我在domain.com/subfolder中有一个开发网站。 当我在编辑器中添加页面链接时,它通常会捕获绝对路径,但是我需要在编辑器中将其设为相对路径(例如:/ page-parent / page-child),然后显示domain.com/subfolder/page-parent / page-child在页面上。
First I tried to add a modified filter indicating the site_url(), but it didnt work for me, the path was still absolute when I was adding a page link in the text.
And then I just used WP wp_make_link_relative hook. It partly worked, but the link (in the editor) was still /subfolder/page-parent/page-child.
第一次尝试
add_filter('wp_insert_post_data', function($data) {
if ($data['post_type'] === 'page') {
$site_url = site_url();
$site_url_relative = preg_replace('~^https?://~', '//', $site_url);
$data['post_content'] = preg_replace("~(<[a-z][^>]+(?:href|src)=\\\\('|\"))$site_url(?=[^\\s'\">]*\\2)~i", "\\1$site_url_relative", $data['post_content']);
}
return $data;
});
第二次尝试
add_filter( 'post_link', 'wp_make_link_relative' ); // Normal post link
add_filter( 'post_type_link', 'wp_make_link_relative' ); // Custom post type link
add_filter( 'page_link', 'wp_make_link_relative' ); // Page link
add_filter( 'attachment_link', 'wp_make_link_relative' ); // Attachment link
add_filter( 'get_shortlink', 'wp_make_link_relative' ); // Shortlink
因此,我希望链接在编辑器中为/ page-parent / page-child,在前端为完整路径(带有site_url)。我在做什么错了?