Opened 4 years ago

Last modified 8 months ago

#10722 assigned enhancement

Allow filtering of whether or not 404 should be handled.

Reported by: prettyboymp Owned by: markjaquith
Priority: normal Milestone: Future Release
Component: General Version: 2.8.5
Severity: normal Keywords: has-patch tested
Cc: prettyboymp, erick@…

Description

There are certain situations where a user may not want the page to always 404 when there are no posts for the given permalink. IE, author pages, dates, etc. There should be a filter available to allow the 404 handling to be bypassed.

Attachments (6)

handle_404.patch (1.2 KB) - added by prettyboymp 4 years ago.
10722.patch (1.1 KB) - added by hakre 3 years ago.
10722.2.patch (640 bytes) - added by prettyboymp 3 years ago.
fixed undefined $handle_404 var
10722.3.patch (652 bytes) - added by prettyboymp 3 years ago.
fixed logic differences
10722.diff (705 bytes) - added by Denis-de-Bernardy 3 years ago.
10722.4.patch (397 bytes) - added by ethitter 8 months ago.
refreshed for 3.4

Download all attachments as: .zip

Change History (23)

comment:1 follow-up: ↓ 2   miqrogroove4 years ago

How about situations where a template finds no posts and wants to force a 404?

comment:2 in reply to: ↑ 1   prettyboymp4 years ago

  • Version set to 2.8.5

Replying to miqrogroove:

How about situations where a template finds no posts and wants to force a 404?

The template can issue a 404 at anytime as long as no content has been sent yet. This patch just allows plugins to keep a 404 from being thrown before the template or plugin has had a chance to handle it how it wants.

  • Milestone changed from Unassigned to 3.0

Since the last patch was created the function did change. I placed the filter now as it was intended in the old patch.

Additionally it was possible to remove duplicate code.

As an improvement compared to the old patch, now, if 404 was de-filtered, 200 is given if !is_404() which was the standard behaviour of the function in pre-filter times.

hakre3 years ago

doesn't WP already return a 200 code when no posts are found for a valid url?

Only if its a tag, category, author, or other queried object. While that covers most of the situations, it doesn't cover all of them, which is why the filter is needed.

The specific instance I needed this was while using a custom post_type of calendar_event where the events for a month were displayed with a permalink structure of /events/year/month/. I didn't want a month returning a 404 just because their weren't any events scheduled for the month yet. My only work around was to falsely set is_category as true on 'posts_selection' then remove it on 'template_redirect'.

you could also hook into the wp hook or during query parsing, too. but yeah, a new filter makes more sense.

fixed undefined $handle_404 var

fixed logic differences

  • Keywords tested added

Tested this against r13305 and it works. Used the following to test:

add_filter('handle_404', 'no_404');  function no_404(){return false;}

Looks like a duplicate of #11312, which was closed as invalid.

To the arguments against the filter in #11312:

The solution of setting $is_404 to false doesn't work since the handle_404 function also checks post count.

Adding a hook to template_redirect to override the 404 status header would also overwriting the no-cache headers that were set. Which, I understand its a simple thing to override those too, but now you have several headers being messed with at different points in the program flow, which is just opening a bigger gap for conflicts.

That sounds like a good argument, prettyboymp. I just made a change in the 404 logic yesterday... seems good to have one place where a plugin can hook in and say "WP, don't send a 404."

  • Owner set to markjaquith
  • Status changed from new to assigned

I attached an alternative patch. (didn't like the logic in the first much...)

  • Milestone changed from 3.0 to Future Release

Punting due to feature freeze.

#21978 was marked as a duplicate.

#21978 was marked as a duplicate.

refreshed for 3.4

  • Cc erick@… added
Note: See TracTickets for help on using tickets.