How To Redirect Headers Before They Are Sent To A Page Using WordPress
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?
Answer
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.
Related Questions
- → CORS missmatch because of http
- → Building sitemap for 2 wordpress install under 1 domain
- → How to remove empty elements after class?(jQuery)
- → Get width of an element and apply to another relative to the first one?
- → How to remove caption p class from wordpress?
- → 301 Redirection from no-www to www in wordpress
- → Laravel 5 routing using prefix
- → WordPress - Header position Top, Left &Right
- → how to add rel=nofollow to some specific external links in wordpress
- → octobercms install error: database is not empty?
- → How to edit the index page of wordpress theme?
- → How to select a Post Type (Wordpress) to pass a filter in head?
- → What sort of URL structure should be used to display AMP HTML vs vanilla HTML