9

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

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

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

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

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

    I added the following code to header.php:

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

Again, no success. Now I am at a dead end. Any ideas?

albert
  • 8,112
  • 3
  • 47
  • 63
user1686582
  • 91
  • 1
  • 2
  • Have you tried adding your code you mentioned in 2. to your `functions.php` file? You mentioned you added it to the `header.php` file when the Wordpress documentation states it should be included in `functions.php`. – Axel Jan 25 '13 at 17:21
  • I created a ticket in WP where asked to add this functionality https://core.trac.wordpress.org/ticket/50441#ticket Also in details of the ticket you may find a security concern. – Sergey Ponomarev Jun 20 '20 at 18:24

3 Answers3

9

You could do it like this with a plugin or by adding to functions. I think that ends up being cleaner.

add_action( 'pre_get_posts', 'add_header_origin', 9 );

function add_header_origin() {
    if (is_feed()){
        header( 'Access-Control-Allow-Origin: *' );
    }
}            
Tom Woodward
  • 1,653
  • 14
  • 21
  • Tiny improvement, using the dedicated action `do_feed_rss2` and setting priority to less than 10 (ref: https://codex.wordpress.org/Plugin_API/Action_Reference#Feed_Actions ) : `add_action( 'do_feed_rss2', 'add_header_origin', 9 );` – RonaldPK Feb 20 '23 at 16:08
2

Copy the original rss-template "wp-includes/feed-rss2.php" to your theme directory and activate it by adding this code to your functions.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
}

Then you can modify your rss-template and add the header like mentioned by jefffederman.

-1

Go to wp-includes/feed-rss2.php and below

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

add

header("Access-Control-Allow-Origin: *");
  • 2
    This solution requires the modification of core Wordpress files, which would be overwritten when Wordpress is upgraded (which happens frequently). I would suggest a solution that uses an action hook within the theme's function.php file to include the additional header. – Axel Jan 25 '13 at 17:19
  • Also add ob_start(); at the top of the page. There is chance that server might have already start sending data to client, after that headers cannot be set. – kiranvj Jan 26 '13 at 06:26
  • Never ever modify core WordPress files. @Axel explains it very well why and what to to to avoid the editing. – Ale Oct 29 '18 at 09:35