How to add CORS support to Wordpress RSS feed?

I am trying to add CORS support ( http://enable-cors.org/ ) to an RSS2 feed in a custom Wordpress theme. I tried the following, all to no avail:

  1. Following the instructions at https://web.archive.org/web/20140314152828/http://bowdenweb.com:80/wp/2011/05/how-to-enable-cors-in-wordpress.html , I tried to modify the theme header.php file and add the following code to it:

    header("Access-Control-Allow-Origin: *");

    This was successful in adding the CORS header to Wordpress posts, but not to the RSS2 feed.

  2. Then I tried to use the "Plugin API / Action Reference", that is, the function add_action( http://codex.wordpress.org/Plugin_API/Action_Reference ).

    I added the following code in header.php:

    function add_cors_headers()
    {
        header("Access-Control-Allow-Origin: *");
    }
    
    add_action('rss2_head','add_cors_headers');
    

, . . ?

+8
3

. , .

add_action( 'pre_get_posts', 'add_header_origin' );

function add_header_origin() {
    if (is_feed()){
        header( 'Access-Control-Allow-Origin: *' );
    }
}            
+4

rss- "wp-includes/feed-rss2.php" , . php:

remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'my_feed_rss2', 10, 1 );

function my_feed_rss2( $for_comments ) {
    $rss_template = get_stylesheet_directory() . '/feed-rss2.php';

    if( file_exists( $rss_template ) )
        load_template( $rss_template );
    else
        do_feed_rss2( $for_comments ); // Call default function
}

rss- , jefffederman.

+2

wp-includes/feed-rss2.php

header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);

header("Access-Control-Allow-Origin: *");
0

All Articles