Opened 9 years ago
Last modified 6 years ago
#36849 new enhancement
Filter to limit length of post slug (filter for _truncate_post_slug)
Reported by: | wp-entwickler.at | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.5.2 |
Component: | Posts, Post Types | Keywords: | |
Focuses: | Cc: |
Description
There are circumstances where you need to limit the lenght of the post slug.
Unfortunately there is no filter in _truncate_post_slug to do so.
It will be a great enhancement, if there would be a filter in this function.
Related function:
-wp_unique_post_slug - calling _truncate_post_slug
Additionally it would be very usefull, if there could be a filter right at the start of wp_unique_post_slug for filtering the slug.
Change History (5)
#1
@
9 years ago
- Component changed from Formatting to Posts, Post Types
- Type changed from feature request to enhancement
#2
@
9 years ago
Thanks for your fast reply!
Sorry, I wasn't able to realize that _truncate_post_slug()
is marked as private.
The filter wp_unique_post_slug
is fired at the very end of the function.
All checks are done at this point. Final.
If you alter the slug with this filter, no additional checks will be made.
So it could happen that you're ending with a corrupt slug.
It would be far better to alter the slug before things like the post_name_check, conflicts_with_date_archive check or the flat slug check were done.
A possible solution could be to filter the hardcoded length ( at the moment this is set to 200 minus some stuff *gg*).
Gruezi
#3
@
9 years ago
Although in the context of this ticket it looks like a bit of a workaround, but adding a filter to wp_insert_post_data
or an action on save_post
could achieve what you are after.
#4
@
9 years ago
Hi Barry! Thanks for your input. I've already solved the problem. No problem here.
My intention was a solution in core. I believe that setting the slug length should not need a workaround.
People alter the length of the excerpt in no time and are used to a use a simple trick for that.
I think the slug should get the same easy handling.
#5
@
9 years ago
@wp-entwickler.at Thanks for the ticket!
wp_unique_post_slug()
truncates to 200 characters because the post_name
column in the wp_posts
table is VARCHAR(200)
. In other words, WP truncates like it does to maintain data integrity, and to provide a smoother experience than simply rejecting post slugs that are too long.
I don't think we can introduce a simple filter for the truncation length, because we need to ensure that slugs still fit in the the database column. In other words, you shouldn't be able to filter to 250
. So we'd need a bit of extra logic.
What is the use case for wanting shorter slugs? If it's something fairly general, it makes the case stronger for adding a filter here.
Hey there,
Welcome to WordPress Trac and thanks for creating this ticket!
Note:
_truncate_post_slug()
is marked as being private and thus should not contain any filters. Instead, a filter for limiting the slug length would need to be added towp_unique_post_slug()
. Though I don't see why this isn't already possible using thewp_unique_post_slug
.