Make WordPress Core

Opened 15 years ago

Closed 8 years ago

Last modified 7 years ago

#10722 closed enhancement (fixed)

Allow filtering of whether or not 404 should be handled.

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


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 15 years ago.
10722.patch (1.1 KB) - added by hakre 15 years ago.
10722.2.patch (640 bytes) - added by prettyboymp 14 years ago.
fixed undefined $handle_404 var
10722.3.patch (652 bytes) - added by prettyboymp 14 years ago.
fixed logic differences
10722.diff (705 bytes) - added by Denis-de-Bernardy 14 years ago.
10722.4.patch (397 bytes) - added by ethitter 12 years ago.
refreshed for 3.4
10722.5.patch (835 bytes) - added by ocean90 9 years ago.

Download all attachments as: .zip

Change History (29)

#1 follow-up: @miqrogroove
15 years ago

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

#2 in reply to: ↑ 1 @prettyboymp
15 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.

#3 @prettyboymp
15 years ago

  • Milestone changed from Unassigned to 3.0

#4 @hakre
15 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.

15 years ago

#5 @Denis-de-Bernardy
15 years ago

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

#6 @prettyboymp
15 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
15 years ago

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

14 years ago

fixed undefined $handle_404 var

14 years ago

fixed logic differences

#8 @chrisscott
14 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
14 years ago

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

#10 @prettyboymp
14 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
14 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
14 years ago

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

#13 @Denis-de-Bernardy
14 years ago

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

#14 @nacin
14 years ago

  • Milestone changed from 3.0 to Future Release

Punting due to feature freeze.

#15 @nacin
12 years ago

#21978 was marked as a duplicate.

#16 @nacin
12 years ago

#21978 was marked as a duplicate.

12 years ago

refreshed for 3.4

#17 @ethitter
12 years ago

  • Cc erick@… added

#18 @nacin
11 years ago

  • Component changed from General to Query

#19 @chriscct7
9 years ago

  • Keywords 2nd-opinion added; tested removed

9 years ago

#20 @ocean90
9 years ago

  • Milestone changed from Future Release to 4.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.


#21 @ocean90
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

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.

#22 @waisir
7 years ago

add_rewrite_endpoint( 'my-page', EP_PERMALINK );
add_rewrite_rule( '^store/checkout', 'index.php?my-page=mtc-checkout', 'top' );
Last edited 7 years ago by waisir (previous) (diff)
Note: See TracTickets for help on using tickets.