wppaste
WordPress

WP::send_headers()

Since
2.0.0, 4.4.0, 6.1.0
Source
wp-includes/class-wp.php:435
Sends additional HTTP headers for caching, content type, etc.

Description

Sets the Content-Type header. Sets the 'error' status (if passed) and optionally exits.
If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed.

Compatibility

WordPress
since 6.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to WP::send_headers() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
119–158

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 300.

Plugin surface
2 hooks

Third-party callbacks on 'wp_headers', 'send_headers' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below WP::send_headers()
  • serializeserialisationmaybe_unserialize()one call below WP::send_headers()
  • querycontent queryget_post()one call below WP::send_headers()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 36 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP::send_headers() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_user_logged_in() && !isset($value) && empty($status)119–137is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent()
!is_user_logged_in() && !isset($value) && empty($status)121–139is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && !empty($status)122–140is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent()
!is_user_logged_in() && !isset($value) && !empty($status)124–142is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && empty($status) && isset($headers) && headers_sent()126–141is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status)128–141is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent()
!is_user_logged_in() && !isset($value) && empty($status) && isset($headers) && headers_sent()128–143is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && empty($status) && isset($headers) && !headers_sent()129–144is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && !empty($status) && isset($headers) && headers_sent()129–144is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status)130–143is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status)131–144is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent()
!is_user_logged_in() && !isset($value) && empty($status) && isset($headers) && !headers_sent()131–146is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), headers_sent(), header_remove(), headers_sent(), exit()
24 further outcomes, up to 158 instructions
!is_user_logged_in() && !isset($value) && !empty($status) && isset($headers) && headers_sent()131–146is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && !empty($status) && isset($headers) && !headers_sent()132–147is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status)133–146is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status)134–147is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent()
!is_user_logged_in() && !isset($value) && !empty($status) && isset($headers) && !headers_sent()134–149is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status) && isset($headers) && headers_sent()135–145is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status)136–149is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status) && isset($headers) && headers_sent()137–147is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status)137–149is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status) && isset($headers) && !headers_sent()138–148is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status) && isset($headers) && headers_sent()138–148is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status)139–151is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && empty($status) && isset($headers) && !headers_sent()140–150is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), headers_sent(), header_remove(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status) && isset($headers) && headers_sent()140–150is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status) && isset($headers) && headers_sent()141–150is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status) && isset($headers) && !headers_sent()141–151is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status) && isset($headers) && headers_sent()143–152is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && !pings_open() && !empty($status) && isset($headers) && !headers_sent()143–153is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status) && isset($headers) && !headers_sent()144–153is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status) && isset($headers) && headers_sent()144–153is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && empty($status) && isset($headers) && !headers_sent()146–155is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), headers_sent(), header_remove(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status) && isset($headers) && headers_sent()146–155is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent(), headers_sent(), exit()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status) && isset($headers) && !headers_sent()147–156is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent()
!is_user_logged_in() && !isset($value) && is_singular() && pings_open() && !empty($status) && isset($headers) && !headers_sent()149–158is_user_logged_in(), feed_content_type(), get_option(), get_lastpostmodified(), mysql2date(), get_lastcommentmodified(), mysql2date(), strtotime(), strtotime(), md5(), strtotime(), is_singular(), pings_open(), get_bloginfo(), apply_filters(), status_header(), headers_sent(), header_remove(), headers_sent(), exit()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev300119–15840
8.5300119–15840
8.4300119–158405 fewer instructions than PHP 8.3
8.3305113–15640
8.2305113–15640
8.1305113–156403 fewer instructions than PHP 7.4
7.4308113–15740

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while WP::send_headers() runs, in this order:

  1. apply_filters( wp_headers )filterline 558 (+123 into the body)

    Filters the HTTP headers before they're sent to the browser.

  2. do_action( send_headers )actionline 590 (+155 into the body)

    Fires once the requested HTTP headers for caching, content type, etc. have been sent.

Uses · 16

Show all 16

Used by · 1

  • WP::main()Sets up all of the variables required by the WordPress environment.

Source code

	public function send_headers() {		global $wp_query; 		$headers       = array();		$status        = null;		$exit_required = false;		$date_format   = 'D, d M Y H:i:s'; 		if ( is_user_logged_in() ) {			$headers = array_merge( $headers, wp_get_nocache_headers() );		} elseif ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {			// Unmoderated comments are only visible for 10 minutes via the moderation hash.			$expires = 10 * MINUTE_IN_SECONDS; 			$headers['Expires']       = gmdate( $date_format, time() + $expires );			$headers['Cache-Control'] = sprintf(				'max-age=%d, must-revalidate',				$expires			);		}		if ( ! empty( $this->query_vars['error'] ) ) {			$status = (int) $this->query_vars['error']; 			if ( 404 === $status ) {				if ( ! is_user_logged_in() ) {					$headers = array_merge( $headers, wp_get_nocache_headers() );				} 				$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );			} elseif ( in_array( $status, array( 403, 500, 502, 503 ), true ) ) {				$exit_required = true;			}		} elseif ( empty( $this->query_vars['feed'] ) ) {			$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' );		} else {			// Set the correct content type for feeds.			$type = $this->query_vars['feed'];			if ( 'feed' === $this->query_vars['feed'] ) {				$type = get_default_feed();			} 			$headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' ); 			// We're showing a feed, so WP is indeed the only thing that last changed.			if ( ! empty( $this->query_vars['withcomments'] )				|| str_contains( $this->query_vars['feed'], 'comments-' )				|| ( empty( $this->query_vars['withoutcomments'] )					&& ( ! empty( $this->query_vars['p'] )						|| ! empty( $this->query_vars['name'] )						|| ! empty( $this->query_vars['page_id'] )						|| ! empty( $this->query_vars['pagename'] )						|| ! empty( $this->query_vars['attachment'] )						|| ! empty( $this->query_vars['attachment_id'] )					)				)			) {				$wp_last_modified_post    = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );				$wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false ); 				if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) {					$wp_last_modified = $wp_last_modified_post;				} else {					$wp_last_modified = $wp_last_modified_comment;				}			} else {				$wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false );			} 			if ( ! $wp_last_modified ) {				$wp_last_modified = gmdate( $date_format );			} 			$wp_last_modified .= ' GMT';			$wp_etag           = '"' . md5( $wp_last_modified ) . '"'; 			$headers['Last-Modified'] = $wp_last_modified;			$headers['ETag']          = $wp_etag; 			// Support for conditional GET.			if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.1.0
Runs after posts have been queried.from the docblock
4.4.0
X-Pingback header is added conditionally for single posts that allow pings.from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.