Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to make Yoast's Custom RSS feeds post display in an add_feed function.

Just putting Yoast's snippet inside add_feed results in:

ERROR: feedname is not a valid feed template.

I have tried two rewrite functions, but to no avail. What could I be missing?

enter image description here

Yoast's function untouched:

<?php
/*
Template Name: Custom Feed
*/

$numposts = 5;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}

function yoast_rss_text_limit($string, $length, $replacer = '...') {
  $string = strip_tags($string);
  if(strlen($string) > $length)
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
  return $string;
}

$posts = query_posts('showposts='.$numposts);

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
<channel>
  <title>Yoast E-mail Update</title>
  <link><a class="linkclass" href="http://yoast.com/">http://yoast.com/</a></link>
  <description>The latest blog posts from Yoast.com.</description>
  <language>en-us</language>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor><a class="linkclass" href="mailto:joost@yoast.com">joost@yoast.com</a></managingEditor>
<?php foreach ($posts as $post) { ?>
  <item>
    <title><?php echo get_the_title($post->ID); ?></title>
    <link><?php echo get_permalink($post->ID); ?></link>
    <description><?php echo '<![CDATA['.yoast_rss_text_limit($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
  </item>
<?php } ?>
</channel>
</rss>

Function one:

function myPlugin_add_feed(  ) {
  global $wp_rewrite;
  add_feed('feedname', 'my_feed');
  add_action('generate_rewrite_rules', 'myPlugin_rewrite_rules');
  $wp_rewrite->flush_rules();
}
add_action('init', 'myPlugin_add_feed');

and function two:

function custom_feed_rewrite($wp_rewrite) {
$feed_rules = array(
'feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1),
'(.+).xml' => 'index.php?feed='. $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'custom_feed_rewrite');
share|improve this question

2 Answers 2

up vote 3 down vote accepted

You have add_feed('feedname', 'my_feed'); but no my_feed function to actually generate the feed output.

Create a my_feed function and have it call the template to generate the feed output. Like this:

function my_feed() {
  include 'path-to-that-template-file.php';
}
add_feed('feedname','my_feed');

Then regenerate your permalinks, one time only, by resaving the permalink settings.

Also, you don't need any of that extra rewrite nonsense at all. Just the add_feed is enough. WP handles the rest, and your feed will be at /feed/feedname.

share|improve this answer
    
Thank you Otto! –  mattrepublic Jul 8 '11 at 0:18
    

First, correct way is to use add_feed(), but you should not touch rewrite. add_feed() handles that by itself.

Second, you didn't provide your callback my_feed() function? I am not sure but from that error message I think it fails to get hooked properly.

share|improve this answer
    
The first thing I tried was wrapping Yoast's script in function my_feed() { // code to output the feed goes here } add_feed('feedname', 'my_feed');. After that did not work I tried each of the two rewrites. This is my first foray into feeds, so thank you for letting me know not to touch the rewrite. –  mattrepublic Jul 7 '11 at 20:05
    
Should there be a callback function? –  mattrepublic Jul 7 '11 at 20:06
    
@M.Taylor in your code my_feed() is callback that will handle feed output (in native WP it includes feed template file from core). Your initial idea seems sound, I am not sure what went wrong with it. –  Rarst Jul 7 '11 at 20:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.