[wp-hackers] Throw 404 error via plugin

Ryan Bilesky rbilesky at gmail.com
Wed Jan 12 23:29:50 UTC 2011


No luck.  I am hooking into init, checking to see if I want to allow the
user to access it.  If not throw a 404 error.

<?php
add_action('init','not_found_test',0);
function not_found_test() {
 global $wp_query;
     if (1=1) {
             status_header('404');
             $wp_query->set_404();
 }
}
?>

On Tue, Jan 11, 2011 at 10:24 PM, Andrew Nacin <wp at andrewnacin.com> wrote:

>  On Tue, Jan 11, 2011 at 10:01 PM, Ryan Bilesky <rbilesky at gmail.com>
> wrote:
>
> > So I am working on a plugin where I want to selectivly throw a 404 error.
> >
> > I try header() and this sets the http header to show a not found error,
> but
> > I get the browsers default 404 error page.
> >
> > I try including the 404.php template file but while it looks like a 404
> > error to the user, the http header status shows 200 ok.
> >
> > I try using both methods together again no luck,.  I want the http status
> > code to show 404 AND to use the 404.php template to show the user.  How
> > does
> > the core do this?
>
>
> The template file doesn't have to do with the headers. You'll want to
> trigger 404 headers via status_header('404') and the 404 template via
> $wp_query->set_404(). But a lot of this depends on what exactly you're
> trying to do.
>
> Nacin
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list