Advertisement
mbis

Fix custom redirect language mismatch

Mar 24th, 2022 (edited)
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. function pm_fix_lang_redirect_mismatch($correct_permalink, $redirect_type, $queried_object) {
  2.     global $pm_query, $permalink_manager_uris, $permalink_manager_redirects;
  3.  
  4.     // Run this function only if "Custom redirect" was detected by original Permalink Manager's function
  5.     if($redirect_type == 'custom_redirect') {
  6.  
  7.         // Get the code of requested language
  8.         $requested_language_code = pll_current_language('slug');
  9.  
  10.         // Extract the requested URI and endpoint
  11.         $uri = $pm_query['uri'];
  12.         $endpoint_value = $pm_query['endpoint_value'];
  13.  
  14.         /**
  15.          * Loop through each of items until its code matches the requested language
  16.          */
  17.         foreach($permalink_manager_redirects as $element => $redirects) {
  18.  
  19.             // Stop the function if the redirect array is malformed or there is no custom permalink set for this item
  20.             if(!is_array($redirects) || empty($permalink_manager_uris[$element])) {
  21.                 continue;
  22.             }
  23.  
  24.             // Check if any of the redirects stored for this item matches the requested URI
  25.             if(in_array($uri, $redirects)) {
  26.                
  27.                 // Check the language code of found item
  28.                 $element_language_code = Permalink_Manager_Language_Plugins::get_language_code($element);
  29.  
  30.                 // Stop the function if the language codes matches
  31.                 if($element_language_code == $requested_language_code) {
  32.  
  33.                     // Post is detected
  34.                     if(is_numeric($element)) {
  35.                         $correct_permalink = get_permalink($element);
  36.                     }
  37.                     // Term is detected
  38.                     else {
  39.                         $term_id = intval(preg_replace("/[^0-9]/", "", $element));
  40.                         $correct_permalink = get_term_link($term_id);
  41.                     }
  42.  
  43.                     if(!empty($correct_permalink)) {
  44.                         return $correct_permalink;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.     }
  50.  
  51.     return $correct_permalink;
  52. }
  53. add_filter('permalink_manager_filter_redirect', 'pm_fix_lang_redirect_mismatch', 0, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement