Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_unique_permalinks($default_uri, $native_slug, $element, $post_name, $native_uri) {
- global $permalink_manager_uris;
- // Ignore native permalinks
- if($native_uri) { return $default_uri; }
- // Store all the custom permalinks in a separate variable
- $uris = $permalink_manager_uris;
- // Exclude this element
- if(!empty($element->ID) && !empty($uris[$element->ID])) {
- unset($uris[$element->ID]);
- } else if(!empty($element->term_id) && !empty($uris[$element->term_id])) {
- unset($uris["tax-{$element->term_id}"]);
- }
- do {
- $duplicates_ids = array_keys($uris, $default_uri);
- $duplicates_ids_count = count($duplicates_ids);
- if(!empty($duplicates_ids_count)) {
- preg_match('/(.+?)(?:-([\d]+))?(\.[^\.]+$|$)/', $default_uri, $parts);
- $index = (!empty($parts[2])) ? $parts[2] + 1 : 2;
- $default_uri = preg_replace('/(.+?)(?:-([\d]+))?(\.[^\.]+$|$)/', '$1-' . $index . '$3', $default_uri);
- }
- } while ($duplicates_ids_count > 0);
- return $default_uri;
- }
- add_filter('permalink_manager_filter_default_post_uri', 'pm_unique_permalinks', 999, 5);
- add_filter('permalink_manager_filter_default_term_uri', 'pm_unique_permalinks', 999, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement