WordPress.org

Ready to get started?Download WordPress

Forums

Display wordpress 404 from my script. (4 posts)

  1. paulsst
    Member
    Posted 5 years ago #

    I have a script inside my blog: http://someblog.com/myscript.php
    everything goes fine but when I open the script
    http://someblog.com/myscript.php?param=NotExistingPage

    the script must show 404 wordpress error page.

    Anyone knows how to call and display wordpress default 404 page? Thanks.

  2. designsimply
    Member
    Posted 5 years ago #

    To change the 404 error page, you can edit the 404.php file for your theme. The WordPress documentation talks about how to copy the default theme 404 page to your theme if that's what you're interested in. See Creating an Error 404 Page.

  3. paulsst
    Member
    Posted 5 years ago #

    No-no, I don't need to edit 404 page. Sorry if I sounded not correct.

    I do need to call the WordPress 404 page from my own script.
    Lets see what I have now: if my script is called with non existing parameter, e.g.:
    http://someblog.com/myscript.php?param=NonExistingValue
    I show that "sorry, such paramenter does not exist"

    Now I don't want to show my own error. I want to show beautiful and default WordPress 404 page from _my_own_script. I guess I need to call
    /%themepath%/404.php.

    How can I call /%themepath%/404.php from my own script and display it? Thanks.

  4. jsdalton
    Member
    Posted 4 years ago #

    This is probably 10 months too late for you, but I just had the same conundrum. Here's the solution I came up with.

    The easiest way to show a 404 page from an arbitrary point in your script would be to define the following function:

    function return_404() {
    	status_header(404);
    	nocache_headers();
    	include( get_404_template() );
    	exit;
    }

    and then just call return_404() from wherever you need to. This should work as long as it is called before the headers are sent (I am personally using it on a template_redirect hook).

    I basically copied the above from the same code WordPress uses to display a 404 page when a post is not found. This is as of 2.8, FYI.

Topic Closed

This topic has been closed to new replies.

About this Topic