У меня есть веб-сайт разработчика в 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) в клиентской части. Что я делаю не так?