How To Redirect Headers Before They Are Sent To A Page Using WordPress

- 1 answer

I am currently learning from the O'Reilly Building Web Apps with Wordpress book, the book explains that the page template is loaded after the WordPress init and wp actions have fired. I know that the wp_head action will not be called until I call get_header.

How do you process and redirect a generic contact form that collects a person's name, email, and message? Or is that not the correct approach of thinking?



Here list of actions that I've recorded to help me when trying to figure out what to hook into with WordPress. This is a very small snippet of my complete list, but it should with what you're trying to do:

theme functions.php loaded

  • after_setup_theme
  • init
  • wp_loaded

wp query vars defined

  • get_search_query (filter)
  • pre_handle_404 (filter)
  • wp
  • template_redirect
  • template_include
  • get_header

headers are sent

start html, start head

  • wp_head
  • wp_enqueue_scripts
  • wp_print_styles
  • wp_print_scripts

end head

The very last action you can rely on is get_header before the headers are sent. As you can see the wp_head fires after the headers are sent, so that is no use to you.

In a case like yours, I typically want to wait until at least the WP Query vars are defined since a lot of times I need to know that information before deciding what to do. I can't comment on your specific scenario, so you may not care. but hooking into any action before the get_header should be fine.

Your hook should be late enough that you allow WordPress to process whatever information you may need. If you don't care about anything but just simply want the $_POST data, then go ahead and hook into the init action. If you want a lot of information processed by WordPress to be available to you, then hook into the wp, etc.