Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#36849 new enhancement

Filter to limit length of post slug (filter for _truncate_post_slug)

Reported by: wp-entwicklerat's profile Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.2
Component: Posts, Post Types Keywords:
Focuses: Cc:


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 @swissspidy
8 years ago

  • Component changed from Formatting to Posts, Post Types
  • Type changed from feature request to enhancement

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 to wp_unique_post_slug(). Though I don't see why this isn't already possible using the wp_unique_post_slug.

8 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*).


Last edited 8 years ago by (previous) (diff)

#3 @barryceelen
8 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.

8 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.

Last edited 8 years ago by (previous) (diff)

#5 @boonebgorges
8 years ago 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.

Note: See TracTickets for help on using tickets.