Using wp_get_http_headers to retrieve HTTP headers in WordPress

The wp_get_http_headers function is a part of WordPress core, designed to retrieve the HTTP headers from a specified URL. It is a function that sends a HTTP request to a URL and returns the response headers. This function can be utilized in scenarios where there is a need to gain information about a particular URL without fetching the entire page content.

For instance, it can be used to check if a certain resource is available at a given URL, to determine the content type of a URL before downloading it, or to check the last modification time of a resource. It can also be used to detect redirections, as the function follows redirections and retrieves headers from the final destination.

The returned value of the wp_get_http_headers function is an associative array where each key is a header name and the corresponding value is the header value. If the function fails to retrieve the headers for any reason, it returns false.

Parameters Accepted by wp_get_http_headers Function

The wp_get_http_headers function in WordPress takes in two parameters. These are:

  • $url (string) – This is a required parameter. Its purpose is to specify the URL from which the HTTP headers are to be retrieved.
  • $deprecated (bool) – This is an optional parameter. Its default value is false. However, it’s important to note that this parameter is not utilized in the function.

Return Value of wp_get_http_headers Function

The wp_get_http_headers function returns either a WpOrgRequestsUtilityCaseInsensitiveDictionary or false. The function will return the HTTP headers in the form of a WpOrgRequestsUtilityCaseInsensitiveDictionary if the operation is successful. In case of a failure, the function will return false.

Examples

How to Retrieve HTTP Headers from a URL

$url = 'https://www.example.com';
$headers = wp_get_http_headers( $url );
if ( $headers !== false ) {
 echo '<pre>';
 print_r( $headers );
 echo '</pre>';
} else {
 echo '<p>Failed to retrieve headers</p>';
}

This code snippet retrieves and prints the HTTP headers from the URL ‘https://www.example.com’ using the wp_get_http_headers() function. If the function fails to retrieve the headers, it will print ‘Failed to retrieve headers’.

How to Check if a URL is Accessible

$url = 'https://www.example.com';
$headers = wp_get_http_headers( $url );
if ( $headers !== false ) {
 echo '<p>The URL is accessible</p>';
} else {
 echo '<p>The URL is not accessible</p>';
}

This code snippet checks if the URL ‘https://www.example.com’ is accessible by attempting to retrieve the HTTP headers using the wp_get_http_headers() function. If the function successfully retrieves the headers, it means that the URL is accessible. If it fails, it means that the URL is not accessible.

How to Retrieve a Specific HTTP Header from a URL

$url = 'https://www.example.com';
$headers = wp_get_http_headers( $url );
if ( $headers !== false ) {
 if ( isset( $headers['Content-Type'] ) ) {
 echo '<p>Content Type: ' . $headers['Content-Type'] . '</p>';
 } else {
 echo '<p>Content-Type header not found</p>';
 }
} else {
 echo '<p>Failed to retrieve headers</p>';
}

This code snippet retrieves the ‘Content-Type’ HTTP header from the URL ‘https://www.example.com’ using the wp_get_http_headers() function. If the ‘Content-Type’ header is found, it will print its value. If it is not found, it will print ‘Content-Type header not found’. If the function fails to retrieve the headers, it will print ‘Failed to retrieve headers’.

Conclusion

The wp_get_http_headers function in WordPress is a tool that retrieves the HTTP headers from a specified URL. This function can be employed to fetch and analyze the HTTP headers sent by the server in response to a request, providing valuable information such as the server’s status, content type, and other metadata. This is particularly beneficial for understanding how a server responds to requests, troubleshooting server issues, and for various other tasks related to HTTP header management.

Related WordPress Functions