Support » Plugins » Hacks » Is it possible to know the page type when "send_headers" Action is called?

  • Resolved Hubert Nguyen

    (@hubertnguyen)


    Hello,

    I would like to setup custom HTTP cache-control headers depending on the type of pages. I thought that I could use the “send_headers” action hook to do that, but unfortunately, it seems that when this hook is called, WordPress does not yet know what type of page it is dealing with, and therefore is_attachment(), is_author(), is_single(), is_category(), is_tag(), is_feed() and is_home() all return “false”.

    Did I miss something? This would be very handy since not all types should be cached for the same length of time. In theory, once “send_headers” has been called, it’s not possible to send additional header information.

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    All the is_*() template tags are set when the query is parsed, much too late to send headers. You can actually send headers after ‘send_headers’ as long as it is done before any HTML is output, such as <!DOCTYPE html>. For example, the ‘wp’ action could be used to send headers which occurs after ‘send_headers’. Not that it helps you in this case.

    Your only hope is to examine the query vars of the WP object passed to your callback function and decide what the page type is based on those. This is exactly what WP_Query does when it sets the template tags. It may help to review the parse_query() method source code in wp-includes/query.php.

    Thread Starter Hubert Nguyen

    (@hubertnguyen)

    I can indeed send headers as long as they are sent before the start of the HTML code, so this will do what I want.

    The other solution that kind of works is to examine the URLs. Because of how the permalink are built on my site, I’m able to distinguish between the home, tags, and categories pretty easily, but for now your proposed solution is the best.

    I could add a call to a function that will setup the headers in each template file, but I wonder if there’s a hook/action that I can use.

    Moderator bcworkz

    (@bcworkz)

    I think ‘wp’ is about the latest action you could use, as templates are loaded after that and what happens after that is variable. Most requests result in header.php being loaded for most themes. That would be the very latest you could send headers, but some requests, such as feeds and track backs, do not use header.php.

    Thread Starter Hubert Nguyen

    (@hubertnguyen)

    You are right, ‘wp’ is the last one that works with what I originally intended to do. I have access to just about everything I need, including the post information, tag and cat names etc. Problem solved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is it possible to know the page type when "send_headers" Action is called?’ is closed to new replies.