Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add CORS support to Wordpress RSS2 feed?

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?

like image 865
user1686582 Avatar asked Sep 20 '12 16:09

user1686582


People also ask

How do I enable CORS on my WordPress REST API?

Setup your header CORS functionphp file add the following code. In the example above, I've set the variable $origin_url to equal the asterisk ( * ), which means all. But in the following if conditional , I'm checking if the environment is in production mode, change the $origin_url value to my main site URL.

How do I resolve CORS issue in WordPress?

Modifying CORS Headers for WordPress REST API Requests rest_send_cors_headers function in WordPress. Access Control Headers For The WordPress REST API by Josh Pollock. Plugin example for setting custom CORS headers on REST API GET requests. Plugin example for setting custom CORS headers on all REST API requests.

How do I customize my WordPress RSS feed?

Edit the RSS Content Settings The RSS Content Settings will add text and links above and below each of your blog posts in the RSS feed for your site. To get started, click on General Settings in the All in One SEO menu, and then click on the RSS Content tab.


2 Answers

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' );

function add_header_origin() {
    if (is_feed()){
        header( 'Access-Control-Allow-Origin: *' );
    }
}            
like image 158
Tom Woodward Avatar answered Oct 17 '22 22:10

Tom Woodward


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.

like image 37
user2068298 Avatar answered Oct 17 '22 21:10

user2068298