• Hi all,

    I’m new to wordpress development and I tried for couple of days to make this work without much success.

    What I want to achieve is to add a custom header with post url as a value when a new post is published or a new comment to that post is added. I’ve seen that for this the nocache_headers() function is used, so I tried to use it like this

    $permalink = get_permalink($post->ID);
    nocache_headers($permalink)

    then, of course, I have changed the actual functions to be like the following (from the wp-includes/functions.php file, changes are in bold):

    /**
     * Gets the header information to prevent caching.
     *
     * The several different headers cover the different ways cache prevention is handled
     * by different browsers
     *
     * @since 2.8.0
     *
     * @uses apply_filters()
     * @return array The associative array of header names and field values.
     */
    function wp_get_nocache_headers(<strong>$permalink</strong>) {
            $headers = array(
                    'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
                    'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
                    'Pragma' => 'no-cache',
                    <strong>'MyHeader' => $permalink</strong>
            );
    
            if ( function_exists('apply_filters') ) {
                    $headers = (array) apply_filters('nocache_headers', $headers);
            }
            $headers['Last-Modified'] = false;
            return $headers;
    }
    
    /**
     * Sets the headers to prevent caching for the different browsers.
     *
     * Different browsers support different nocache headers, so several headers must
     * be sent so that all of them get the point that no caching should occur.
     *
     * @since 2.0.0
     * @uses wp_get_nocache_headers()
     */
    function nocache_headers(<strong>$permalink</strong>) {
            $headers = wp_get_nocache_headers(<strong>$permalink</strong>);
    
            unset( $headers['Last-Modified'] );
    
            // In PHP 5.3+, make sure we are not sending a Last-Modified header.
            if ( function_exists( 'header_remove' ) ) {
                    @header_remove( 'Last-Modified' );
            } else {
                    // In PHP 5.2, send an empty Last-Modified header, but only as a
                    // last resort to override a header already sent. #WP23021
                    foreach ( headers_list() as $header ) {
                            if ( 0 === stripos( $header, 'Last-Modified' ) ) {
                                    $headers['Last-Modified'] = '';
                                    break;
                            }
                    }
            }
    
            foreach( $headers as $name => $field_value )
                    @header("{$name}: {$field_value}");
    }

    as a result the custom header is created, but it is empty without any value, so I’m wondering what I’m doing wrong here?
    I have tried also with

    global $post;
    $permalink = get_permalink($post->ID);
    nocache_headers($permalink)

    and got the same result.

    Any help is greatly appreciated, or any other method to achieve the above.

    Thanks!

  • The topic ‘how to add $post->ID to nocache_headers()’ is closed to new replies.