Created
January 25, 2023 21:14
-
-
Save HDDen/1763bf14e3187c15f04d0e9b7cd501ff to your computer and use it in GitHub Desktop.
Get Wordpress working on multiple domains with single instance, with correct Yoast canonical, open graph, schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp-config: | |
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] ); | |
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] ); | |
plugin/functions.php: | |
<?php | |
if (!defined('ABSPATH')){ | |
die(); | |
} | |
/** | |
* Фильтры Yoast | |
*/ | |
add_filter( 'wpseo_canonical', function ($canonical) { | |
return csmedsmpcore_changeDomainInLink($canonical); | |
}); | |
add_filter( 'wpseo_opengraph_url', function ($url, $presentation = false){ | |
return csmedsmpcore_changeDomainInLink($url); | |
}); | |
add_filter( 'wpseo_adjacent_rel_url', function($canonical, $rel = false, $presentation = false){ | |
return csmedsmpcore_changeDomainInLink($canonical); | |
}); | |
add_filter( 'wpseo_opengraph_image', function($image){ | |
return csmedsmpcore_changeDomainInLink($image); | |
}); | |
add_filter( 'wpseo_json_ld_search_url', function($search_url){ | |
return csmedsmpcore_changeDomainInLink($search_url); | |
}); | |
add_filter( 'wpseo_schema_graph', function($data, $context = false){ | |
$data = csmedsmpcore_recursive_array_search('https://', $data, function($input){ | |
return csmedsmpcore_changeDomainInLink($input); | |
}); | |
return $data; | |
}); | |
/** | |
* Заменяет домен в переданном url на текущий | |
*/ | |
function csmedsmpcore_changeDomainInLink($url, $targetDomain = false){ | |
// текущий домен | |
if (!$targetDomain){ | |
$target_domain = strtok($_SERVER['HTTP_HOST'], ':'); | |
} | |
// извлекаем домены | |
$url_parts = parse_url($url); | |
if (array_key_exists('host', $url_parts)){ | |
$url = str_ireplace($url_parts['host'], $target_domain, $url); | |
} | |
return $url; | |
} | |
/** | |
* Рекурсивно ищет значение в многомерном массиве и применяет к нему коллбэк | |
*/ | |
function csmedsmpcore_recursive_array_search($needle, &$haystack, $callback){ | |
if (is_array($haystack)){ | |
foreach ($haystack as &$value){ | |
$value = csmedsmpcore_recursive_array_search($needle, $value, $callback); | |
} | |
} else { | |
$haystack = call_user_func($callback, $haystack); | |
} | |
return $haystack; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment