Opened 13 months ago

Closed 11 months ago

Last modified 11 months ago

#20480 closed defect (bug) (fixed)

Can't access original slug with wp_unique_post_slug filter

Reported by: bradyvercher Owned by: nacin
Priority: normal Milestone: 3.5
Component: Permalinks Version: 3.3
Severity: normal Keywords: has-patch
Cc:

Description

This is a follow up to #14111.

The slug passed to the 'wp_unique_post_slug' filter may already be suffixed by the time it's accessible. This requires the default suffix algorithm to be reversed if a developer wants access to the original slug in order to apply their own uniqueness algorithm.

It would be great if the original slug was passed as a parameter in the current filter, or if a new filter was added before the default uniqueness algorithms are executed.

Attachments (2)

20480.patch (620 bytes) - added by johnbillion 12 months ago.
20480.2.diff (1.3 KB) - added by coffee2code 11 months ago.
Adds wp_unique_post_slug filter as proposed in 20480.patch; adds pre_wp_unique_post_slug filter; adds some phpDoc

Download all attachments as: .zip

Change History (9)

  • Severity changed from trivial to normal
  • Type changed from enhancement to defect (bug)
  • Version changed from 3.3.1 to 3.3

I'd class this as a bug. The filter has limited use without passing the original slug.

  • Keywords has-patch added

Patch

That should do it. Thanks!

As an enhancement, something like a pre_wp_unique_post_slug filter that operates similar to this patch might also be beneficial so plugins can bypass the default processing.

Adds wp_unique_post_slug filter as proposed in 20480.patch; adds pre_wp_unique_post_slug filter; adds some phpDoc

+1

As @johnbillion said, the wp_unique_post_slug filter is of limited, or at least diminished, use if the original slug isn't also passed. If passed, then any hooked functions could easily detect if wp_unique_post_slug() modified the incoming slug due to a conflict and could act accordingly (perhaps using different slug renaming logic or for logging).

And as @bradyvercher most recently mentioned, a pre_wp_unique_post_slug filter could be useful and justified here for a few reasons:

  • The wp_unique_post_slug() function (and thus the filter) is not high use, so won't impact performance
  • wp_unique_post_slug() performs database queries (perhaps repeatedly until a unique slug is found), all of which may be rendered moot if a hooking function's intent is to implement its own slug generation/handling logic. The pre slug could short-circuit that unnecessary effort.

Attached 20480.2.diff which amends original patch to add pre filter and some phpDocs.

comment:5 follow-up: ↓ 7   nacin11 months ago

  • Milestone changed from Awaiting Review to 3.5

Sounds good. Can we consider the pre_* filter in a separate ticket?

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [21177]:

Take the original slug passed into wp_unique_post_slug() and pass it to the wp_unique_post_slug filter. props johnbillion. fixes #20480.

comment:7 in reply to: ↑ 5   coffee2code11 months ago

Replying to nacin:

Sounds good. Can we consider the pre_* filter in a separate ticket?

Opened as #21112.

Note: See TracTickets for help on using tickets.