<?php
/**
* Function to attach response header metadata for testing.
*
* @param WP_Error $error WP_Error object containing error information.
*/
function test_response_headers( WP_Error $error ) {
// Check if there's an error
if ( ! empty( $error ) ) {
// Add custom header for testing
header( 'X-Test-Response: Error occurred' );
// Add more metadata as needed
header( 'X-Test-Status: ' . $error->code );
header( 'X-Test-Message: ' . $error->message );
header( 'X-Test-Type: ' . $error->type );
// Optionally, add other relevant metadata
if ( isset( $error->data ) ) {
header( 'X-Test-Data: ' . json_encode( $error->data ) );
}
// Output error message
echo 'Error: ' . esc_html( $error->message );
} else {
// Add custom header for successful response
header( 'X-Test-Response: Success' );
// Add metadata for successful response
header( 'X-Test-Status: 0' );
header( 'X-Test-Message: Operation completed successfully' );
// Optionally, add other relevant metadata
// header( 'X-Test-Data: ' . json_encode( $data ) );
echo 'Success!';
}
exit; //Important: Stop script execution after headers are sent.
}
/**
* Example usage within a WordPress context.
*
* @param WP_Error $error WP_Error object.
*/
add_action( 'template_redirect', 'test_response_headers' );
?>
Add your comment