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:
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.
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?
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.
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.
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.
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: *' );
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With