fix agroexpert links

This commit is contained in:
argoexpert press
2025-04-22 19:14:37 +03:00
parent 5aeb9f4cdb
commit 3d8a4a5054
3 changed files with 39 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
<?php
function fix_agroexpert_links($content) {
$needle = 'https://agroexpert.press';
$position = 0;
while (($pos = strpos($content, $needle, $position)) !== false) {
// Найдём конец ссылки (до пробела, кавычки или закрывающего тега)
$end = $pos + strlen($needle);
while (isset($content[$end]) && !in_array($content[$end], ['"', "'", ' ', "\n", "\r", '<', '>'])) {
$end++;
}
// Вырежем саму ссылку
$link = substr($content, $pos, $end - $pos);
// Если не заканчивается на / и нет query/anchor
if (!str_ends_with($link, '/') && !str_contains($link, '?') && !str_contains($link, '#')) {
$link_fixed = $link . '/';
$content = substr_replace($content, $link_fixed, $pos, strlen($link));
$position = $pos + strlen($link_fixed); // Продвигаемся дальше
} else {
$position = $end;
}
}
return $content;
}
add_filter('the_content', 'fix_agroexpert_links');