3

I have created custom rewrite rules. These rules now go to 404 page as expected. I can use action hook template_redirect to change the template matching query_var.

But I don't want to create separate template files as the page structure depend on the current theme in use. Rather I just want to change the content and title. I have tried change the content using the_content filter, but it seems the filter doesn't even get called for 404 page.

I also tried loading index.php template and then using the_content filter, but it also doesn't work.

Is there any way to achieve this?

11
  • why is a 404 page expected? you shouldn't be getting a 404 if you've done things correctly. 404 pages have no content, no queried object, no loop, etc..
    – Milo
    Jan 15, 2014 at 21:59
  • @Milo These rewrites do not go to any actual page/post. So, virtual pages. How should I handle these types of rewrite?
    – sakibmoon
    Jan 15, 2014 at 22:01
  • You should be able to use the 404.php template page, or am I misunderstanding your question?
    – hornj
    Jan 15, 2014 at 22:02
  • @hornj 404.php is used by default as these aren't actual page. But I want to keep the original page structure of the theme and only to change the title and content.
    – sakibmoon
    Jan 15, 2014 at 22:05
  • well there is no content to change on a 404 page, I suggest a different approach where you provide an option to designate an actual page as the target for these virtual pages.
    – Milo
    Jan 15, 2014 at 22:06

2 Answers 2

1

I have a 404 plugin that does basically what you're needing (if I understand correctly), only it creates a new page (using the same templates from your theme) and registers it as the 404 page. If you already have an existing WordPress page (NOT php file) then you could use code similar to the following to turn it into your new 404 page. Note: You do need to customize this a bit. See the notes in the code below.

//redirect on 404
function redirect_404() {
    global $options, $wp_query;
    if ($wp_query->is_404) {
        $page_title = $this->options['404_page_title'];//replace with your page title
        $redirect_404_url = esc_url(get_permalink(get_page_by_title($page_title))); 
        wp_redirect( $redirect_404_url );
        exit();
    }
}

//Make sure proper 404 status code is returned
function is_page_function() {
    global $options;
    $page_title = $this->options['404_page_title'];//replace with your page title
    if (is_page($page_title)) {
        header("Status: 404 Not Found");
    }
    else {
        return;
    }
}

//Register Hooks
add_action( 'template_redirect', 'redirect_404');
add_action('template_redirect', 'is_page_function');

If you want to checkout the full plugin code you can do so here: http://wordpress.org/plugins/404-silent-salesman/

Hope that helps!

3
  • Thanks for your answer, but if I understand correctly it redirects the 404 page to another page. But that's not what I am looking.
    – sakibmoon
    Jan 16, 2014 at 11:55
  • I guess I'm not quite understanding. Is this a php file you're wanting to direct someone to, or a page created in WordPress? If it's a page generated by WordPress, then all you have to do is insert that page title in the commented areas above and it should do the trick for you. Jan 16, 2014 at 14:37
  • Nevermind, I see you're NOT using a WordPress generated page. I had the same struggle when I was building this plugin and came to the conclusion that the method above is the only real way I could find to make this work. Jan 16, 2014 at 15:01
0

It seems it is not possible from Wordpress point of view as @Milo pointed out

There is no content to change on a 404 page, I suggest a different approach where you provide an option to designate an actual page as the target for these virtual pages.

So, I had to change my plan. From the plugin admin, I kept an option to integrate a page as handler for all the request and sending different content matching query variables.

1
  • I did provide an existing page (editing the loop with pre_guet_posts) and it didn't work because as soon as a successful query is run, the status returned is 301. Now the question is how to throw a 404 page for an EXISTING page: wordpress.stackexchange.com/questions/147344/…
    – sergio
    Jun 3, 2014 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.