fix agroexpert links
This commit is contained in:
30
filters/fix_agroexpert_links.php
Normal file
30
filters/fix_agroexpert_links.php
Normal 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');
|
||||
Reference in New Issue
Block a user