I updated handle_buffer to check the wp_query object so the header is not inserted inappropriately.
Updated list of properties to check on wp_query