Advertisement
mbis

Wildcard permalinks

Apr 22nd, 2020
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.65 KB | None | 0 0
  1. function pm_wildcard_permalinks($element_id, $uri_parts) {
  2.     global $permalink_manager_uris;
  3.  
  4.     if(empty($element_id) && !empty($uri_parts['uri']) && strpos($uri_parts['uri'], 'product/') !== false) {
  5.         $product_slug = basename($uri_parts['uri']);
  6.  
  7.         $regex = sprintf("product\/(.*)\/%s$", preg_quote($product_slug, '/'));
  8.         $matches = preg_grep("/{$regex}/i", $permalink_manager_uris);
  9.  
  10.         if(!empty($matches)) {
  11.             $matches = array_flip($matches);
  12.             $new_element_id = reset($matches);
  13.         }
  14.     }
  15.  
  16.     return (!empty($new_element_id)) ? $new_element_id : $element_id;
  17. }
  18. add_action('permalink_manager_detected_element_id', 'pm_wildcard_permalinks', 9999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement