Sending HTTP HEAD requests in WordPress using wp_remote_head

The wp_remote_head function in WordPress is used to retrieve the HTTP headers from a specified URL. It performs an HTTP HEAD request, which is similar to a GET request but only fetches the headers instead of the entire content.

This function can be beneficial for checking the status of a URL or to inspect specific headers without downloading the full response. It can be particularly useful for:

  • Verifying if a URL is accessible and returning the appropriate HTTP status code.
  • Checking the content type or other relevant headers of a URL.
  • Determining the size of a resource by inspecting the Content-Length header.
  • Inspecting caching headers like Cache-Control or Expires.

The function returns an array containing the response headers, response code, and other relevant data, which can be processed further according to the needs of the application.

Parameters

  • $url (string), required. The URL to retrieve.
  • $args (array), optional. Default: array(). The request arguments.

Return Value

The function returns an array or a WP_Error. It provides the response or a WP_Error in case of failure.

Examples

How to Check if a URL is Reachable

This code snippet demonstrates how to use the wp_remote_head function to check if a given URL is reachable. It sends a HEAD request to the specified URL and checks the response status code to determine if the URL is accessible.

$url = 'https://example.com';
$response = wp_remote_head($url);

if (is_wp_error($response)) {
 echo 'Error: ' . $response->get_error_message();
} else {
 $status_code = wp_remote_retrieve_response_code($response);
 if ($status_code === 200) {
 echo 'URL is reachable.';
 } else {
 echo 'URL is not reachable. Status code: ' . $status_code;
 }
}

How to Retrieve Headers from a URL

This code snippet shows how to use the wp_remote_head function to retrieve the headers from a specified URL. It sends a HEAD request and extracts the headers from the response.

$url = 'https://example.com';
$response = wp_remote_head($url);

if (is_wp_error($response)) {
 echo 'Error: ' . $response->get_error_message();
} else {
 $headers = wp_remote_retrieve_headers($response);
 echo 'Headers: ';
 print_r($headers);
}

How to Check Content Type of a URL

This code snippet illustrates how to use the wp_remote_head function to check the content type of a specified URL. It sends a HEAD request and retrieves the Content-Type header from the response.

$url = 'https://example.com';
$response = wp_remote_head($url);

if (is_wp_error($response)) {
 echo 'Error: ' . $response->get_error_message();
} else {
 $headers = wp_remote_retrieve_headers($response);
 $content_type = isset($headers['content-type']) ? $headers['content-type'] : 'Unknown';
 echo 'Content Type: ' . $content_type;
}

Conclusion

The wp_remote_head function in WordPress facilitates making HTTP HEAD requests, enabling developers to retrieve headers from a specified URL without downloading the entire content. This function is particularly effective for tasks such as verifying the existence of a resource, checking the status of a URL, and obtaining metadata information. By leveraging wp_remote_head, developers can efficiently interact with external resources and optimize their WordPress applications’ performance and functionality.

Related WordPress Functions