Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Method 1. Force the unique custom permalinks
- */
- function pm_force_unique_permalinks($uris) {
- $duplicates = array_count_values($uris);
- foreach($duplicates as $uri => $count) {
- if($count == 1) { continue; }
- $ids = array_keys($uris, $uri);
- foreach($ids as $index => $id) {
- if($index > 0) {
- $uris[$id] = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $index . '$2', $uri);
- }
- }
- }
- return $uris;
- }
- add_filter('permalink_manager_uris', 'pm_force_unique_permalinks', 10);
- /**
- * Method 2. Make the default permalinks unique
- */
- function pm_make_uris_unique($default_uri, $original_slug, $element, $custom_slug, $native_uri) {
- global $permalink_manager_uris;
- // if($native_uri ) { return $default_uri; }
- // Get the element ID
- $element_id = (!empty($element->taxonomy)) ? "tax-{$element->term_taxonomy_id}" : $element->ID;
- // Check if the same URI was used for another post or term
- $all_uris = $permalink_manager_uris;
- if(in_array($default_uri, $all_uris)) {
- unset($all_uris[$element_id]);
- $duplicates = array_count_values($all_uris);
- $duplicates_count = (isset($duplicates[$default_uri])) ? $duplicates[$default_uri] : 0;
- if($duplicates_count) {
- $suffix = intval($duplicates_count) + 1;
- $default_uri = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $suffix . '$2', $default_uri);
- }
- }
- return $default_uri;
- }
- add_filter('permalink_manager_filter_default_post_uri', 'pm_make_uris_unique', 99, 5);
- add_filter('permalink_manager_filter_default_term_uri', 'pm_make_uris_unique', 99, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement