One permalink can be set more than once
-
One permalink can be set more than once, which causes errors.
My permalinks are set to posttitle.
New wordpress install. First I have created the page with title test. Url was /test.
Then an post with ttilte test. Permalink manager set the url also to /test. If i try now to access /test it shows me the post.
-
Hi @aceone999,
Whenever you use the duplicated URI, an alert should be displayed in URI Editor.
Also, you can check a full list of duplicated permalinks inside the plugin admin pages:
Nevertheless, this part of UI must be improved. In the next version of plugin I am going to make it more apparent for users that the particular URI is duplicated. Currently this information might be easily overlooked.
I plan a site with a form where anonymous users can post some things. So they could override easily static pages that way? Well it’s WordPress, but this makes no sense to me. ?? Why should this plugin not just create another valid permalink, like test-1? That way no permalink can ever be invalid. It would be a real enhancement WordPress.
You can always warn the user about what happened. But why destroy things in the first place?`
Hi @aceone999,
There is one particular reason why I made it like this. First of all, with my plugin you can edit the full permalink and not only the native slug (the native slug is always unique and cannot be assigned to more than one post/term). This feature is widely used as it allows to reuse the same slug when the full URL address is unique as a whole:
https://permalinkmanager.pro/docs/tutorials/duplicate-permalinks-slug/For that reason, I think that it is better to nag the user and display the alert instead of forcing the numeral appendixes added to the end of URL.
A few months ago I prepared a custom snippet that could be useful if your particular case. Whenever the custom permalink is used more than once, my plugin will add a numeral suffix to the new default permalink:
https://pastebin.com/CSX9cWnqthanks. I tested the first method for me, but no luck. I still can create posts with the same permalink.
Btw. the warning “URI is already in use, please select another one!” is easily hidden behind the browser auto fill suggestions, if any.The first method will not block the possibility of duplicating the permalink. Basically, it will make the duplicated permalink unique by adding the appendix with numeral. It will be added only after the duplicate is saved.
Exactly. Well, I could still name a side /test/ (duplicate permalink) and it’s still has the permalink /test. (permalink manager is still installed)
Let me show you how it works.
1. Add a new post and save it with a duplicated URI (that is already used by older post):
2. Save it – my plugin will add the numeral to the end of permalink:
As you can see, the alert is displayed when someone is trying to use the same permalink more than once. It is not possible to make the permalink unique (by appending the numeral) before the post is saved.
Unfortunately I see no ideal solution here. Now an alert is displayed, but I see no technical possibility of dynamic filtering the input value. It is not possible to disable the possibility of typing the same permalink in the input field by the user. This can be done only in the back-end.
The users can type/paste anything in the input field. As you know, the permalinks can be fully customized with my plugin and all the magic happens in the back-end.
Eventually, the input value will be parsed once they save the post. Only then, the plugin can sanitize it and make it unique. This is how the below snippet works.
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);
There is no other way than to warn the user about the duplicate and “fix” it in the back-end (during saving process) if he wants to use a duplicated permalinks. As I mentioned, I am going to improve UI and make “the duplicate alert” more apparent.
- This reply was modified 4 years, 6 months ago by Maciej Bis.
- This reply was modified 4 years, 6 months ago by Maciej Bis. Reason: Snippet added
What you explain there, is exactly what not happened on my wordpress. And there is already a page called /test. i could just name another post also /test. Nothing was changed, nothing happened, and the code is still in functions.php.
- This reply was modified 4 years, 6 months ago by aceone999.
I will try to delete any cache I could find on there server, maybe that helps.
Ok, please keep me updated. The snippet filters the array with custom permalinks, so after you add and save a new /test/ post, the custom permalink should be then automatically changed to /test-1/.
If nothing helps, could you try to use this snippet instead? This would allow to manipulate directly the value of $permalink_manager_uris global.
function pm_force_unique_permalinks() { global $permalink_manager_uris; $duplicates = array_count_values($permalink_manager_uris); foreach($duplicates as $uri => $count) { if($count == 1) { continue; } $ids = array_keys($permalink_manager_uris, $uri); foreach($ids as $index => $id) { if($index > 0) { $permalink_manager_uris[$id] = preg_replace('/(.+?)(\.[^\.]+$|$)/', '$1-' . $index . '$2', $uri); } } } } add_action('init', 'pm_force_unique_permalinks', 10);
Tested it today again. Well, it only works on new posts/pages. If you rename an exiting post/page to an existing permalink, this snippset does nothing for me. It just creates a second permalink with the same name.
On my test i deleted title form an permalink before saving.- This reply was modified 4 years, 6 months ago by aceone999.
With your new snippset I it affected existing posts/pages too!
- The topic ‘One permalink can be set more than once’ is closed to new replies.