View difference between Paste ID: 9k5c9Hee and afdnQshi
SHOW: | | - or go back to the newest paste.
1
function pm_unique_permalinks($default_uri, $native_slug, $element, $post_name, $native_uri) {
2
	global $permalink_manager_uris;
3
4
	// Ignore native permalinks
5
	if($native_uri) { return $default_uri; }
6
7
	// Store all the custom permalinks in a separate variable
8
	$uris = $permalink_manager_uris;
9
10
	// Exclude this element
11
	if(!empty($element->ID) && !empty($uris[$element->ID])) {
12
		unset($uris[$element->ID]);
13
	} else if(!empty($element->term_id) && !empty($uris[$element->term_id])) {
14
		unset($uris["tax-{$element->term_id}"]);
15
	}
16
17
	do {
18
		$duplicates_ids = array_keys($uris, $default_uri);
19
		$duplicates_ids_count = count($duplicates_ids);
20
21
		if(!empty($duplicates_ids_count)) {
22
			preg_match('/(.+?)(?:-([\d]+))?(\.[^\.]+$|$)/', $default_uri, $parts);
23
			$index = (!empty($parts[2])) ? $parts[2] + 1 : 2;
24
25
			$default_uri = preg_replace('/(.+?)(?:-([\d]+))?(\.[^\.]+$|$)/', '$1-' . $index . '$3', $default_uri);
26
		}
27
	} while ($duplicates_ids_count > 0);
28
29
	return $default_uri;
30
}
31
add_filter('permalink_manager_filter_default_post_uri', 'pm_unique_permalinks', 999, 5);
32
add_filter('permalink_manager_filter_default_term_uri', 'pm_unique_permalinks', 999, 5);