Make WordPress Core

Opened 17 years ago

Closed 10 years ago

Last modified 9 years ago

#10722 closed enhancement (fixed)

Allow filtering of whether or not 404 should be handled.

Reported by: prettyboymp Owned by: markjaquith
Priority: normal Milestone: 4.5
Component: Query Version: 2.8.5
Severity: normal Keywords: has-patch 2nd-opinion
Cc: Focuses:

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 (7)

handle_404.patch (1.2 KB ) - added by prettyboymp 17 years ago.
10722.patch (1.1 KB ) - added by hakre 17 years ago.
10722.2.patch (640 bytes ) - added by prettyboymp 16 years ago.
fixed undefined $handle_404 var
10722.3.patch (652 bytes ) - added by prettyboymp 16 years ago.
fixed logic differences
10722.diff (705 bytes ) - added by Denis-de-Bernardy 16 years ago.
10722.4.patch (397 bytes ) - added by ethitter 14 years ago.
refreshed for 3.4
10722.5.patch (835 bytes ) - added by ocean90 11 years ago.

Download all attachments as: .zip

Change History (29)

#1 follow-up: @miqrogroove
17 years ago

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

#2 in reply to: ↑ 1 @prettyboymp
17 years ago

  • Version2.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.

#3 @prettyboymp
17 years ago

  • Milestone Unassigned3.0

#4 @hakre
17 years ago

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.

@hakre
17 years ago

#5 @Denis-de-Bernardy
17 years ago

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

#6 @prettyboymp
17 years ago

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

#7 @Denis-de-Bernardy
17 years ago

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

@prettyboymp
16 years ago

fixed undefined $handle_404 var

@prettyboymp
16 years ago

fixed logic differences

#8 @chrisscott
16 years ago

  • 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;}

#9 @nacin
16 years ago

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

#10 @prettyboymp
16 years ago

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.

#11 @markjaquith
16 years ago

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

#12 @nacin
16 years ago

  • Owner set to markjaquith
  • Status newassigned

#13 @Denis-de-Bernardy
16 years ago

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

#14 @nacin
16 years ago

  • Milestone 3.0Future Release

Punting due to feature freeze.

#15 @nacin
14 years ago

#21978 was marked as a duplicate.

#16 @nacin
14 years ago

#21978 was marked as a duplicate.

@ethitter
14 years ago

refreshed for 3.4

#17 @ethitter
14 years ago

  • Cc erick@… added

#18 @nacin
13 years ago

  • Component GeneralQuery

#19 @chriscct7
11 years ago

  • Keywords 2nd-opinion added; tested removed

@ocean90
11 years ago

#20 @ocean90
11 years ago

  • Milestone Future Release4.5

10722.5.patch introduces a filter pre_handle_404 to allow short-circuiting the status handling.

In GlotPress we don't use the main query at all. Therefore, if you have no published posts each request gets handled as a 404 error.

Thoughts?

#21 @ocean90
10 years ago

  • Resolutionfixed
  • Status assignedclosed

In 36629:

In WP::handle_404() introduce a filter pre_handle_404 to short-circuit default header status handling.

This comes handy if you use WordPress without posts, means $wp_query contains no results.

Props prettyboymp, hakre, prettyboymp, Denis-de-Bernardy, ethitter, ocean90.
Fixes #10722.

Note: See TracTickets for help on using tickets.