WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 2 months ago

#16557 new enhancement

Ability to disable redirect_guess_404_permalink()

Reported by: msafi Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 3.1
Component: Canonical Keywords: has-patch dev-feedback commit
Focuses: Cc:

Description

Can you make redirect_guess_404_permalink() pluggable or have its return value pass-through a filter so that developers can override it?

I know I can remove_filter('template_redirect', 'redirect_canonical') but redirect_canonical is too useful to be disabled. Only disabling URL guessing would be great.

Thanks a lot,

MK

Attachments (4)

filters to tweak permalink guessing.diff (1.8 KB) - added by simonwheatley 4 years ago.
One possibility to allow filtering the where clauses for posts and pages
new filter to replace redirect_guess_404_permalink.diff (705 bytes) - added by simonwheatley 4 years ago.
New filter to replace redirect_guess_404_permalink
16557.diff (1.6 KB) - added by simonwheatley 8 months ago.
Refresh patch, rename filter, add test
16557.2.diff (2.4 KB) - added by DrewAPicture 4 months ago.
Refresh + hook docs

Download all attachments as: .zip

Change History (29)

comment:1 @msafi4 years ago

  • Cc msafi added

comment:2 follow-up: @nacin4 years ago

  • Keywords needs-patch added; URL guessing URL redirect removed
  • Milestone changed from Awaiting Review to Future Release

+1. In fact I thought I already created this ticket. It should probably be attached into redirect_canonical() through a filter.

Whipped this up not long ago, as a workaround:

function remove_redirect_guess_404_permalink( $redirect_url ) {
    if ( is_404() )
        return false;
    return $redirect_url;
}
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );

comment:3 @msafi4 years ago

Oh, this is a good idea, thanks!

@simonwheatley4 years ago

One possibility to allow filtering the where clauses for posts and pages

comment:4 @simonwheatley4 years ago

  • Cc simon@… added
  • Keywords has-patch dev-feedback added; needs-patch removed
  • Version changed from 3.0.5 to 3.1

Added a possible patch which separates the where clauses into page related and post related, and allows plugin devs to filter either snippet of SQL.

Patch created against 3.1.1.

comment:5 @nacin4 years ago

What I'd like to do is simply make redirect_guess_404_permalink() actually hook into redirect_canonical(), I guess as a filter since it returns data. Then the filter can be removed.

So instead of:

if ( ! $redirect_url )
	$redirect_url = redirect_guess_404_permalink();

Instead do:

$redirect_url = apply_filters( 'redirect_guess_404_permalink', $redirect_url );

And hook the function into there. Then it may be removed. Also, the filter name is lame -- perhaps can be more generic considering the positioning.

@simonwheatley4 years ago

New filter to replace redirect_guess_404_permalink

comment:6 @simonwheatley4 years ago

@nacin - Thanks for the steer. Is the attached diff what you were looking for?

comment:7 follow-up: @westi4 years ago

Replying to nacin:

What I'd like to do is simply make redirect_guess_404_permalink() actually hook into redirect_canonical(), I guess as a filter since it returns data. Then the filter can be removed.

So instead of:

if ( ! $redirect_url )
	$redirect_url = redirect_guess_404_permalink();

Can we not just hook redirect_guess_404_permalink() onto redirect_canonical and be done with it - no new filters needed?

Instead do:

$redirect_url = apply_filters( 'redirect_guess_404_permalink', $redirect_url );

And hook the function into there. Then it may be removed. Also, the filter name is lame -- perhaps can be more generic considering the positioning.

Version 0, edited 4 years ago by westi (next)

comment:8 in reply to: ↑ 7 @simonwheatley4 years ago

Replying to westi:

Can we not just hook redirect_guess_404_permalink() onto redirect_canonical and be done with it - no new filters needed?

That would involve moving the point that the redirect guessing is done, which (from limited testing just now) means the current guessing behaviour doesn't happen. I favour the additional filter as per my most recent patch.

comment:9 @mboynes23 months ago

  • Cc mboynes@… added

I'd love to see this finally make its way into core. Is there anything in particular holding it up?

comment:10 @joostdevalk20 months ago

I see no reason why the first, very simple, patch could not just be committed and make everybody happy....

comment:11 @Lex_Robinson16 months ago

Could this be merged please? I need it.

comment:12 @MikeSchinkel14 months ago

In preparing for my WordCamp Atlanta talk about URL routing I've run into this function causing all kind of headache for customized URLs.

There's a reason for the HTTP 404 status code, incorrect URLs are incorrect and the client searching for them should be told so! :)

The problem with this is it can return one resource today and another resource in the future after the user added a higher priority "guess."

My vote is (for my own sites at least) to fully disable this functionality Please address this so we can use plugins to fully control our URLs.

Thanks!

P.S. Related #19693

comment:13 @SergeyBiryukov14 months ago

  • Keywords 4.0-early added

comment:14 in reply to: ↑ 2 @haukep12 months ago

I stumbled upon this feature request while researching a solution for the issue outlined here: http://wordpress.stackexchange.com/questions/144937/disable-only-url-auto-complete-not-the-whole-canonical-url-system

Replying to nacin:

Whipped this up not long ago, as a workaround:

function remove_redirect_guess_404_permalink( $redirect_url ) {
    if ( is_404() )
        return false;
    return $redirect_url;
}
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );

So for all people eagerly waiting for better control over how the redirect guess is performed, I took nacin's code above and turned it into a small plugin. The plugin still waits for its review but should hopefully be available here at some point: https://wordpress.org/plugins/disable-url-autocorrect-guessing/

comment:15 @paulschreiber9 months ago

I, too, would find this helpful.

comment:16 @SergeyBiryukov9 months ago

#29598 was marked as a duplicate.

@simonwheatley8 months ago

Refresh patch, rename filter, add test

comment:17 @simonwheatley8 months ago

New patch:

  • Renames the new filter to 404_redirect
  • Adds a test in canonical.php

comment:18 @DrewAPicture8 months ago

  • Keywords needs-docs added

The new filter will need hook documentation.

comment:19 @simonwheatley8 months ago

Darnit I had hook docs on that, must have got lost in some revert. I blame my head cold. Good spot, will do!

comment:20 @DrewAPicture4 months ago

  • Keywords commit added; 4.0-early needs-docs removed
  • Milestone changed from Future Release to 4.2

16557.2.diff refreshes the patch following [31168] to move the add_filter() call to default-filters.php. Also adds hook docs. Moving to 4.2 for consideration.

@DrewAPicture4 months ago

Refresh + hook docs

comment:21 @slackbot3 months ago

This ticket was mentioned in Slack in #core by drew. View the logs.

comment:22 @DrewAPicture3 months ago

16557.2.diff still applies and unit tests pass.

comment:23 @slackbot3 months ago

This ticket was mentioned in Slack in #core by drew. View the logs.

comment:24 @slackbot2 months ago

This ticket was mentioned in Slack in #core by drew. View the logs.

comment:25 @DrewAPicture2 months ago

  • Milestone changed from 4.2 to Future Release

Seems like this could use a little more consideration. The hook name could be improved and @ocean90 expressed reluctance about the efficacy of the unit test. Let's try this in a future release.

Note: See TracTickets for help on using tickets.