Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_fix_lang_redirect_mismatch($correct_permalink, $redirect_type, $queried_object) {
- global $pm_query, $permalink_manager_uris, $permalink_manager_redirects;
- // Run this function only if "Custom redirect" was detected by original Permalink Manager's function
- if($redirect_type == 'custom_redirect') {
- // Get the code of requested language
- $requested_language_code = pll_current_language('slug');
- // Extract the requested URI and endpoint
- $uri = $pm_query['uri'];
- $endpoint_value = $pm_query['endpoint_value'];
- /**
- * Loop through each of items until its code matches the requested language
- */
- foreach($permalink_manager_redirects as $element => $redirects) {
- // Stop the function if the redirect array is malformed or there is no custom permalink set for this item
- if(!is_array($redirects) || empty($permalink_manager_uris[$element])) {
- continue;
- }
- // Check if any of the redirects stored for this item matches the requested URI
- if(in_array($uri, $redirects)) {
- // Check the language code of found item
- $element_language_code = Permalink_Manager_Language_Plugins::get_language_code($element);
- // Stop the function if the language codes matches
- if($element_language_code == $requested_language_code) {
- // Post is detected
- if(is_numeric($element)) {
- $correct_permalink = get_permalink($element);
- }
- // Term is detected
- else {
- $term_id = intval(preg_replace("/[^0-9]/", "", $element));
- $correct_permalink = get_term_link($term_id);
- }
- if(!empty($correct_permalink)) {
- return $correct_permalink;
- }
- }
- }
- }
- }
- return $correct_permalink;
- }
- add_filter('permalink_manager_filter_redirect', 'pm_fix_lang_redirect_mismatch', 0, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement