WP::send_headers()
- Since
- 2.0.0, 4.4.0, 6.1.0
- Source
wp-includes/class-wp.php:435
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
- Scaling
- Scales with input
- Instructions
- 119–158
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 300.
Third-party callbacks on 'wp_headers', 'send_headers' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below WP::send_headers() - serializeserialisation
maybe_unserialize()one call below WP::send_headers() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_user_logged_in() && !isset($value) && empty($status) | 119–137 | is_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–139 | is_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–140 | is_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–142 | is_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–141 | is_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–141 | is_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–143 | is_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–144 | is_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–144 | is_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–143 | is_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–144 | is_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–146 | is_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–146 | is_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–147 | is_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–146 | is_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–147 | is_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–149 | is_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–145 | is_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–149 | is_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–147 | is_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–149 | is_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–148 | is_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–148 | is_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–151 | is_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–150 | is_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–150 | is_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–150 | is_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–151 | is_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–152 | is_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–153 | is_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–153 | is_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–153 | is_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–155 | is_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–155 | is_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–156 | is_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–158 | is_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 300 | 119–158 | 40 | |
| 8.5 | 300 | 119–158 | 40 | |
| 8.4 | 300 | 119–158 | 40 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 305 | 113–156 | 40 | |
| 8.2 | 305 | 113–156 | 40 | |
| 8.1 | 305 | 113–156 | 40 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 308 | 113–157 | 40 |
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:
- apply_filters( wp_headers )filterline 558 (+123 into the body)
Filters the HTTP headers before they're sent to the browser.
- 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
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- wp_get_nocache_headers()Gets the HTTP header information to prevent caching.
- get_option()Retrieves an option value based on an option name.
- get_default_feed()Retrieves the default feed.
- feed_content_type()Returns the content type for specified feed type.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- mysql2date()Converts given MySQL date string into a different format.
- get_lastpostmodified()Gets the most recent time that a post on the site was modified.
- get_lastcommentmodified()Retrieves the date the last comment was modified.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
- pings_open()Determines whether the current post is open for pings.
Show all 16
- get_bloginfo()Retrieves information about the current site.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- status_header()Sets HTTP status header.
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
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.
Signature, return type and hooks compared across 5 parsed releases.
X-Pingback header is added conditionally for single posts that allow pings.from the docblockAbout 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.