WordPress.org

Make WordPress Core

Ticket #23021: wp_header.diff

File wp_header.diff, 1.5 KB (added by andy, 7 years ago)

New utility: wp_header($name, $value)

  • wp-includes/functions.php

     
    783783}
    784784
    785785/**
     786 * Queue or unqueue HTTP headers
     787 * Error suppression in use. Fails silently when headers already sent.
     788 *
     789 * @since 3.5.1
     790 *
     791 * @param string $name Header field name or entire header
     792 * @param string|bool $value Optional header value.
     793 *        If $value is not set then $name is sent verbatim.
     794 *        If $value is false then $name is removed or sent blank.
     795 */
     796function wp_header( $name, $value = null ) {
     797        if ( $value === null ) {
     798                @header( $name );
     799        } elseif ( $value === false ) {
     800                // Remove the header
     801                if ( function_exists( 'header_remove' ) ) {
     802                        // header_remove was added in PHP 5.3
     803                        @header_remove( $name );
     804                } else {
     805                        // Prior to PHP 5.3, calling header("Name:") is the only way to empty a header.
     806                        // However, sometimes servers do crazy things like convert an empty Last-Modified to 1970.
     807                        // So we don't want to send empty headers unless we must do so to override a previously set header.
     808                        $lname = strtolower( $name );
     809                        $len = strlen( $lname );
     810                        foreach ( headers_list() as $header ) {
     811                                if ( strtolower( substr( $header, 0, $len ) ) == $lname ) {
     812                                        @header( "{$name}:" );
     813                                        break;
     814                                }
     815                        }
     816                }
     817        } else {
     818                @header( "$name: $value" );
     819        }
     820}
     821
     822/**
    786823 * Retrieve the description for the HTTP status.
    787824 *
    788825 * @since 2.3.0