Get WordPress page slug in send_headers action

I want to send custom headers (disable cache) with send_headers action on a specific page but cannot access the page slug. The $post variable seems to be empty. The code is as follows.

add_action('send_headers', 'add_header_came');

function add_header_came() {
        global $post;
        if($post->post_name == 'page1' || $post->post_name == 'page2') {
            header("Cache-Control: no-cache, must-revalidate");
            header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
        }
    }

Read more here: Get WordPress page slug in send_headers action

Leave a Reply

Your email address will not be published. Required fields are marked *