Created
September 15, 2023 00:47
-
-
Save crazyyy/2e114e40ad98fcd573b7f337d2b1b269 to your computer and use it in GitHub Desktop.
Revisions
-
crazyyy created this gist
Sep 15, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ This code modifies various URLs in WordPress generated by the Yoast SEO plugin to replace the domain with the current domain. Here's a breakdown of what the code does: 1. The code adds filters to various Yoast SEO functions in the `functions.php` file to modify specific URLs. These filters ensure that the URLs are updated with the current domain. 2. The `change_domain_in_link` function is a callback function used by the filters. It takes a URL as input and replaces the domain in the URL with the current domain. It uses the `str_ireplace` function to perform the replacement. 3. The `recursive_array_search_callback` function is a helper function used by the `change_domain_in_links` function. It recursively searches for a specific value in a multidimensional array and applies a callback function to the found value. In this case, it is used to search for URLs starting with "https://" in the schema graph data. 4. The `change_domain_in_links` function modifies the URLs in the schema graph data generated by Yoast SEO. It uses the `recursive_array_search_callback` function to search for URLs starting with "https://" and replaces the domain in those URLs with the current domain. Overall, this code ensures that URLs generated by Yoast SEO, such as canonical URLs, Open Graph URLs, and schema graph URLs, are updated with the current domain. This can be useful when migrating a WordPress site to a new domain or when working with different environments. This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ <?php // First make changes to wp-config.php: //define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] ); //define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] ); //and functions.php: add_filter('wpseo_canonical', 'change_domain_in_link'); add_filter('wpseo_opengraph_url', 'change_domain_in_link'); add_filter('wpseo_adjacent_rel_url', 'change_domain_in_link'); add_filter('wpseo_opengraph_image', 'change_domain_in_link'); add_filter('wpseo_json_ld_search_url', 'change_domain_in_link'); add_filter('wpseo_schema_graph', 'change_domain_in_links'); /** * Replaces the domain in the given URL with the current domain. * * @param string $url The original URL. * @return string The modified URL. */ function change_domain_in_link($url) { $target_domain = strtok($_SERVER['HTTP_HOST'], ':'); $url_parts = parse_url($url); if (isset($url_parts['host'])) { $url = str_ireplace($url_parts['host'], $target_domain, $url); } return $url; } /** * Recursively searches for a value in a multidimensional array and applies a callback to it. * * @param mixed $needle The value to search for. * @param array $haystack The array to search in. * @param callable $callback The callback function to apply to the found value. * @return array The modified array. */ function recursive_array_search_callback($needle, $haystack, $callback) { foreach ($haystack as &$value) { if (is_array($value)) { $value = recursive_array_search_callback($needle, $value, $callback); } else { $value = call_user_func($callback, $value); } } return $haystack; } /** * Modifies the URLs in the schema graph data generated by Yoast SEO. * * @param array $data The original schema graph data. * @param mixed $context The context (optional). * @return array The modified schema graph data. */ function change_domain_in_links($data, $context = false) { $target_domain = strtok($_SERVER['HTTP_HOST'], ':'); $data = recursive_array_search_callback('https://', $data, function ($input) use ($target_domain) { return str_ireplace(parse_url($input, PHP_URL_HOST), $target_domain, $input); }); return $data; }