Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_force_unique_permalinks($post_id, $new_uri, $old_uri) {
- global $permalink_manager_uris;
- while(empty($new_unique_permalink)) {
- $duplicates = array_keys($permalink_manager_uris, $new_uri);
- if(is_array($duplicates)) {
- $duplicated_ids = array_flip($duplicates);
- // Ignore this particular post
- if(isset($duplicated_ids[$post_id])) {
- unset($duplicated_ids[$post_id]);
- }
- // Count duplicates
- $duplicates_count = count($duplicated_ids) + 1;
- // Add numeral prefix to the URL
- if($duplicates_count > 1) {
- preg_match('/(.*)-([\d]{1,3})$/', $new_uri, $out);
- if(!empty($out[2])) {
- $new_uri = sprintf('%s-%d', $out[1], (int) $out[2] + 1);
- } else {
- $new_uri = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $duplicates_count . '$2', $new_uri);
- }
- } else {
- $new_unique_permalink = $new_uri;
- }
- }
- }
- $permalink_manager_uris[$post_id] = $new_unique_permalink;
- update_option('permalink-manager-uris', $permalink_manager_uris);
- }
- // Make the new post's permalink unique
- add_action('permalink_manager_new_post_uri', 'pm_force_unique_permalinks', 10, 3);
- // Make the existing permalink unique after the permalink is updated
- add_action('permalink_manager_updated_post_uri', 'pm_force_unique_permalinks', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement