• Resolved slolo

    (@slolo)


    Hello,

    I am trying to modify http headers with your plugin.

    So, I have created a simple snippet which do this:

    add_filter('wp_headers', 'my_custom_headers');

    function my_custom_headers($headers) {
    // Add X-XSS-Protection header
    if (!isset($headers['X-XSS-Protection'])) {
    $headers['X-XSS-Protection'] = '1; mode=block;';
    }

    // Add X-Content-Type-Options header
    if (!isset($headers['X-Content-Type-Options'])) {
    $headers['X-Content-Type-Options'] = 'nosniff';
    }

    return $headers;
    }

    But, $headers variables only contain “Content-Type” value.

    In fact I would like to be able to check if headers have already being sent (from .htaccess by example) and be able to modify them.

    So, what is the way to get http headers in order to control them with WPCode snippet?

    Thanks in advance for your help and have a nice day.

Viewing 1 replies (of 1 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @slolo,

    WPCode does not change anything in the way the wp_headers filter works, we use an early hook (plugins_loaded) to load the snippet if you use the location “Run Everywhere” so you should be able to send headers at that time as it runs before any output is made by PHP.

    The wp_headers filter will only return the headers that have been set through WordPress but if you add a PHP header by directly using the PHP header function you will not see that reflected in the wp_headers filter. You can see how that filter is used in the source of WordPress core: https://github.com/WordPress/wordpress-develop/blob/6.5/src/wp-includes/class-wp.php#L558-L558

    Regarding seeing other headers you may try to use the PHP function getallheaders or similar functions. This is not specific to WPCode or even WordPress.

    Lastly, depending on what you want to do with these headers please consider that if your site uses any type of Page Cache, the headers you set in PHP will not be reflected in the cached version of the pages as that is going to be a static version of the page.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.