Opened 4 years ago
Last modified 8 months ago
#10722 assigned enhancement
Allow filtering of whether or not 404 should be handled.
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (23)
prettyboymp — 4 years ago
comment:1
follow-up:
↓ 2
miqrogroove — 4 years ago
comment:2
in reply to:
↑ 1
prettyboymp — 4 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.
comment:3
prettyboymp — 3 years ago
- 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.
doesn't WP already return a 200 code when no posts are found for a valid url?
comment:6
prettyboymp — 3 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'.
you could also hook into the wp hook or during query parsing, too. but yeah, a new filter makes more sense.
comment:8
chrisscott — 3 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;}
comment:10
prettyboymp — 3 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.
comment:11
markjaquith — 3 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."
comment:12
nacin — 3 years ago
- Owner set to markjaquith
- Status changed from new to assigned
Denis-de-Bernardy — 3 years ago
I attached an alternative patch. (didn't like the logic in the first much...)
comment:14
nacin — 3 years ago
- Milestone changed from 3.0 to Future Release
Punting due to feature freeze.
comment:15
nacin — 8 months ago
#21978 was marked as a duplicate.
comment:16
nacin — 8 months ago
#21978 was marked as a duplicate.
comment:17
ethitter — 8 months ago
- Cc erick@… added

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