Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#50387 closed defect (bug) (invalid)

Cannot modify header information - headers already sent for "nocache_headers()"

Reported by: malthert's profile malthert Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.5
Component: General Keywords:
Focuses: Cc:

Description

function nocache_headers needs to be modified to not try to set/remove headers when the headers were already sent to avoid generating a notice.

Optionally it would also make sense to change the return type to bool, so we can return false in case the headers were already sent.

Suggestion (would be better with return false; though):

function nocache_headers() {
	if ( headers_sent() ) {
		return;
	}

Change History (3)

#1 @williampatton
3 years ago

Hi @malthert,

Thank you for opening this ticket and sorry you ran into a problem here.

The nocache_headers() function actually does already contain an early bail the same as your suggested change. See: https://developer.wordpress.org/reference/functions/nocache_headers/#source

Is there something else wrong with the function that you think could be improved? Is your suggestion only about returning false?

If we are changing the returns in the function personally I would prefer a descriptive WP_Error object be returned so it could proved feedback about what is going wrong.

#2 @malthert
3 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

I didn't realise the project I was on was on an older WP version of this file/function.
Ticket can be closed, unless one wants to modify the return value to make it more descriptive.
However there's nothing that can be done if it returns false/error anyway; that's why it's probably not worth to change it.

#3 @SergeyBiryukov
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution changed from wontfix to invalid

Thanks for the follow-up!

For reference, the headers_sent() check was added to nocache_headers() in [45611] / #47632 for WordPress 5.3.

Note: See TracTickets for help on using tickets.