| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Apply security headers. |
| 9 |
* |
| 10 |
* This function applies several HTTP security headers to enhance site protection if the |
| 11 |
* 'ns_shield_security_headers' option is enabled. The following headers are applied: |
| 12 |
* - Strict-Transport-Security: Applied only if the connection is secure (HTTPS). |
| 13 |
* - X-Frame-Options: SAMEORIGIN |
| 14 |
* - X-Content-Type-Options: nosniff |
| 15 |
* - Referrer-Policy: no-referrer-when-downgrade |
| 16 |
* - Permissions-Policy: geolocation=(self), microphone=() |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
function ns_shield_apply_security_headers() { |
| 21 |
if ( get_option( 'ns_shield_security_headers' ) ) { |
| 22 |
// Apply HSTS header only if connection is secure. |
| 23 |
if ( is_ssl() ) { |
| 24 |
header( "Strict-Transport-Security: max-age=31536000; includeSubDomains; preload" ); |
| 25 |
} |
| 26 |
header( "X-Frame-Options: SAMEORIGIN" ); |
| 27 |
header( "X-Content-Type-Options: nosniff" ); |
| 28 |
header( "Referrer-Policy: no-referrer-when-downgrade" ); |
| 29 |
header( "Permissions-Policy: geolocation=(self), microphone=()" ); |
| 30 |
} |
| 31 |
} |
| 32 |
add_action( 'send_headers', 'ns_shield_apply_security_headers' ); |
| 33 |
?> |
| 34 |
|