0

I'm making a custom RSS feed, which outputs tags in particular namespaces. When I output <pugpig:summary> for example, the pugpig part disappears and I'm left with <summary> in the output tag. How is WordPress doing this and how can I stop it?

Here is my custom feed logic:

add_action('init', 'ns_add_custom_rss_feeds');

function ns_add_custom_rss_feeds(){
ns_create_pugpig_feed();
ns_create_content_feed();
ns_create_issue_feed();
ns_create_google_currents_feed();
}

/**
* Create the new pugpig feed
* add_feed params: feed name and callback funcion
*/
function ns_create_pugpig_feed(){
add_feed('pugpig', 'ns_get_pugpig_template');
}

/**
* Callback function to create the feed
* get_template_part link to a separate template file to keep the functionality separate to the layout.
* the file will be: /wp-content/themes/template/rss-pugpig.php
*/
function ns_get_pugpig_template(){
get_template_part('rss2', 'pugpig');
}

And here is where I output the tags:

/**
* RSS2 Feed Template for displaying pugpig feed.
*
* @package newscientist
*/

//URL: http://www.newscientist.com/feed/pugpig (for current issue)
//URL: http://www.newscientist.com/feed/pugpig?issue=1234 (for a given issue)

$issue = get_query_var( 'issue' );

if(empty($issue)){
//if the parameter "issue" is not in the URL, we take the current issue
$issue = ns_get_current_issue_number();
}

$issue_info = ns_get_issue_info($issue);
$volume = $issue_info -> field('volume');

$args = array(
'post_type' => 'article',
'meta_query' => array(
array(
'key' => 'issue_number',
'value' => $issue
),
array(
'key' => 'pugpig',
'value' => '1'
)
),
'posts_per_page'=>-1
);
//query posts by type, issue_number and pugpig flag
$posts = query_posts( $args );

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';

<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:media="http://search.yahoo.com/mrss/" xmlns:pugpig="http://schema.pugpig.com/rss/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:php="http://www.w3.org/1999/XSL/Transform" version="2.0">
<channel>
<title><?php bloginfo_rss('name'); ?> - Vol <?php echo $volume; ?> Issue <?php echo $issue; ?></title>
<?php echo("<link>"); self_link(); echo("</link>\n"); ?>
<description>RSS feed for Volume <?php echo $volume; ?> Issue <?php echo $issue; ?></description>
<?php while(have_posts()) : the_post(); ?>
<?php
$page = get_post_meta( $post->ID, 'page', true );
$authors = get_post_meta( $post->ID, 'authors', true )
?>
<item>
<title><?php the_title_rss(); ?></title>
<author><?php the_author(); ?></author>
<?php
if ( ! empty ( $authors )) {
ns_print_authors( $post->ID, $authors );
}
?>
<?php
if (get_post_meta($post->ID,"standfirst_use_strap",true)){
?>
<?php echo("<pugpig:summary><p class=\"strap\">");echo strip_tags(the_excerpt_rss()); echo("</p></pugpig:summary>\n");
} else {
echo("<pugpig:summary><p class=\"strap\">");
echo get_post_meta($post->ID,"standfirst",true);
echo ("</p></pugpig:summary>\n");
}
?>
<?php echo("<pugpig:subtitle>"); ns_print_taxonomy( $post->ID, 'article_type');
echo ("</pugpig:subtitle>\n");
echo("<green:issue>"); echo $issue; echo("</green:issue>\n");
echo("<pugpig:page>"); echo $page; echo("</pugpig:page>\n");
$article_section = wp_get_object_terms($post->ID,'article_section');
$article_types = wp_get_object_terms($post->ID, 'article_subject');
$istech = false;
$isnews = false;
foreach($article_section as $section){
if ($section->name == 'News'){ $isnews = true;}
}
foreach($article_types as $subject){
if ($subject->name =='Technology'){ $istech = true; }
}

if ($istech && $isnews ){
?><category>Technology</category><?php
} else {
?>
<category><?php ns_print_taxonomy( $post->ID, 'article_section'); ?></category>
<?php
}
?>
<?php echo("<pugpig:thumbnail>");
echo get_post_meta($post->ID, '_wp_article_thumbnail', true);
echo("</pugpig:thumbnail>\n");
echo("<pugpig:topics>"); ns_print_taxonomy( $post->ID, 'article_topic'); echo("</pugpig:topics>\n");
echo("<pugpig:channels>"); ns_print_taxonomy( $post->ID, 'article_subject'); echo("</pugpig:channels>\n");
?>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php ns_print_pubdate( $post->ID ); ?></pubDate>
<guid isPermaLink="false"><?php ns_print_guid (get_the_guid()) ?></guid>
<description>
<?php
if (!get_post_meta($post->ID,"standfirst_use_strap",true)){
echo '<p class="infuse">'.strip_tags(get_the_excerpt()).'</p>';
} else {
if (get_post_meta("standfirst",true));
echo '<p class="note">'.get_post_meta($post->ID,"standfirst",true).'</p>';
}
?>
<?php ns_print_description(get_the_content_feed('rss2'));
?></description>

<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
<?php
/*
* Print pubdate.
* it will be embargo_date if set. Otherwise, it will be post date.
*/
function ns_print_pubdate( $post_id ) {
//mysql2date('F d Y ', get_post_time('Y-m-d H:i:s', true), false);
$embargo_date = get_post_meta($post_id, 'embargo_date', true);
$creation_date = get_post_time('Y-m-d H:i:s', $post_id);
$publication_date = empty($embargo_date) ? $creation_date : $embargo_date;

echo mysql2date('F d Y ', $publication_date, false);
}

/*
* Print description.
* Process the body to adapt it to pugpig content.
* Decode html characters
*/

function ns_print_description ( $content ) {
//Instead of get_teh_content(), use get_the_content_feed('rss2') and fix empty tags
$content = str_replace("–","-",$content);
$content = str_replace(" "," ",$content);
$content = str_replace("…","...",$content);
$content = str_replace("&colon;",":",$content);
$content = preg_replace("#<script.*?>.*?</script>#","",$content); // remove javascript from description
$final_content = str_replace('<p>', '<p class="infuse">', $content);
$final_content = str_replace('<div class=\'box-out\'>', '<div class=\'artbx bxbg\'>', $final_content);
$final_content = str_replace('<p class=\'credit\'>', '<p class=\'copyright\'>', $final_content);
$final_content = preg_replace('#\[/?caption.*?\]#',"",$final_content);

$callback3 = function($matches){
$imgdata = $matches[3];
$matches1 = array();
preg_match('#src="(.*?)"#',$imgdata,$matches1);
$src = $matches1[1];
$matches1 = array();
preg_match('#title="(.*?)"#',$imgdata,$matches1);
$title = $matches1[1];
$matches1 = array();
preg_match('#class="(.*?)"#',$imgdata,$matches1);
$class = $matches1[1];
$matches1 = array();
preg_match('#alt="(.*?)"#',$imgdata,$matches1);
$alt = $matches1[1];
$matches1 = array();
preg_match('#wp-image-(\d+)#',$class,$matches1);
$imgnumber = $matches1[1];
$credit ="";

if ($matches[2]){ $title = $matches[2]; }
if ($matches[4]){
$matches1 = array();
preg_match('/<figcaption>(.*?)<\/figcaption>/',$matches[4],$matches1);
$caption = $matches1[1];
}
if ($matches[5]){
$matches1 = array();
preg_match('/<p\sclass="copyright">(.*?)</p>',$matches[5],$matches1);
$credit = $matches1[1];
}
if ($imgnumber && !$credit){
$credit = get_post_meta($imgnumber,'credit',true);
}
if (preg_match('/graphic/',$class)){
return "<figure class=\"infographic article-img-left\"><img src=\"$src\"/><figcaption>$caption</figcaption>";
}
return "<figure><img class=\"$class\" src=\"$src\"/><figcaption>$caption</figcaption><p class=\"copyright\">$credit</p></figure>";
};

$callback2 = function($matches){
$imgdata = $matches[2];
$matches1 = array();
preg_match('#src="(.*?)"#',$imgdata,$matches1);
$src = $matches1[1];
$matches1 = array();
preg_match('#title="(.*?)"#',$imgdata,$matches1);
$title = $matches1[1];
$matches1 = array();
preg_match('#class="(.*?)"#',$imgdata,$matches1);
$class = $matches1[1];
$matches1 = array();
preg_match('#alt="(.*?)"#',$imgdata,$matches1);
$alt = $matches1[1];
$matches1 = array();
preg_match('#wp-image-(\d+)#',$class,$matches1);
$imgnumber = $matches1[1];
$credit ="";

if ($matches[2]){ $title = $matches[2]; }
if ($matches[3]){ $credit = $matches[3]; }
$caption = $matches[3];
$credit = $matches[4];
if (preg_match('/graphic/',$class)){
return "<figure class=\"infographic article-img-left\"><img src=\"$src\"><figcaption>$caption</figcaption>";
}
return "<figure><img class=\"$class\" src=\"$src\"><figcaption>$caption</figcaption><p class=\"copyright\">$credit</p></figure>";
};
$final_content = preg_replace_callback("#<figure(.*?)><img(.*?)\"/><div class=\"image-details\"><figcaption>(.*?)</figcaption><p class='credit'>(.*?)</p></div></figure>#",$callback3,$final_content);

$callback1 = function($matches){
$imgdata = $matches[1];
$matches1 = array();
preg_match('#src="(.*?)"#',$imgdata,$matches1);
$src = $matches1[1];
$matches1 = array();
preg_match('#title="(.*?)"#',$imgdata,$matches1);
$title = $matches1[1];
$matches1 = array();
preg_match('#class="(.*?)"#',$imgdata,$matches1);
$class = $matches1[1];
$matches1 = array();
preg_match('#alt="(.*?)"#',$imgdata,$matches1);
$alt = $matches1[1];
$matches1 = array();
preg_match('#wp-image-(\d+)#',$class,$matches1);
$imgnumber = $matches1[1];
$credit ="";

if ($matches[2]){ $title = $matches[2]; }
if ($matches[3]){ $credit = $matches[3]; }
if ($imgnumber && !$credit){
$credit = get_post_meta($imgnumber,'credit',true);
}
if (!preg_match('/graphic/',$class)){
return "<figure><img class=\"$class\" src=\"$src\" alt=\"$alt\" title=\"$title\"><figcaption>$title</figcaption><p class=\"copyright\">$credit</p></figure>";
}
return "<figure class=\"infographic\ article-img-left\"><img src=\"$src\" alt=\"$alt\" title=\"$title\"><figcaption>$title</figcaption><p class=\"copyright\">$credit</p></figure>";
};
$final_content = preg_replace_callback('#<img(.*?)>([^<]+)<i>([^<]+)</i>#',$callback1,$final_content);

$final_content = preg_replace_callback("#<figure(.*?)\"><h3>(.*?)</h3><img(.*?)><div\sclass=\"images-detail\">(<figcaption>.*?<figcaption>)?(<p\sclass=\"image-descrption\">.*?</p>)(<p\sclass=\"copyright\">.*?</p>)<div></figure>#",$callback3,$final_content);

$callback = function($matches){
$imgdata = $matches[1];
$matches1 = array();
preg_match('#src="(.*?)"#',$imgdata,$matches1);
$src = $matches1[1];
$matches1 = array();
preg_match('#title="(.*?)"#',$imgdata,$matches1);
$title = $matches1[1];
$matches1 = array();
preg_match('#class="(.*?)"#',$imgdata,$matches1);
$class = $matches1[1];
$matches1 = array();
preg_match('#alt="(.*?)"#',$imgdata,$matches1);
$alt = $matches1[1];
$matches1 = array();
preg_match('#wp-image-(\d+)#',$class,$matches1);
$imgnumber = $matches1[1];
$credit ="";
if ($matches[2]){ $title = $matches[2]; }
if ($matches[3]){ $credit = $matches[3]; }
if ($imgnumber && !$credit){
$credit = get_post_meta($imgnumber,'credit',true);
}
return "<figure><img class=\"$class\" src=\"$src\" alt=\"$alt\" title=\"$title\"><figcaption>$title<p class=\"copyright\">$credit</p></figcaption></figure>";
};

$final_content = preg_replace_callback('#(?<!<figure).*?<img(.*?)>([^<]+)<i>([^<]+)</i></p>#',$callback,$final_content);
#   $final_content = preg_replace_callback('#(?<!<figure).*?<img(.*?)>#',$callback,$final_content);
$final_content = preg_replace('#&([^;]{1,8})#','&$1',$final_content);
$final_content = preg_replace('/</','<',$final_content);
$final_content = preg_replace('/>/','>',$final_content);
$final_content = ns_decode_characters( $final_content );
$final_content = preg_replace('/<figure class="article-img-inline">/','<figure>',$final_content);
$final_content = preg_replace('/<figure class="article-img-right">/','<figure>',$final_content);
$final_content = preg_replace('#<img(.*?)>#','<img$2>',$final_content);
echo $final_content;

}

// Print authors
function ns_print_authors( $post_id, $authors ){
for ($i = 0; $i < $authors; $i++) {
$author_name = get_post_meta($post_id, 'authors_'.$i.'_name', true);
$author_affiliation = get_post_meta($post_id, 'authors_'.$i.'_affiliation', true);
$author_affiliation = ns_decode_characters( $author_affiliation);

$author_location = get_post_meta($post_id, 'authors_'.$i.'_location', true);
$author_location = ns_decode_characters( $author_location);

echo '<pugpig:author>';
echo '<pugpig:aname>'.$author_name.'</pugpig:aname>';
echo '<pugpig:alocation>'.$author_location.'</pugpig:alocation>';

echo '<pugpig:affiliation>'.$author_affiliation.'</pugpig:affiliation>';
echo '</pugpig:author>';
}
}

// Print taxonomy
function ns_print_taxonomy ( $post_id, $taxonomy ) {
$article_types = wp_get_object_terms($post_id, $taxonomy);
$output = '';
if(!empty($article_types) && !is_wp_error( $article_types )){
$count = 0;
foreach($article_types as $type){
if ($count == 0) {
$output = $type->name;
} else {
$output = $output.', '.$type->name;
}
$count++;
}
}
echo $output;
}

// Print guid
function ns_print_guid ( $guid) {
$dir = substr(strrchr($guid, "/"), 1);
echo $dir;
1
  • 2
    Can you please format your code before expecting us to wade through it. It's the least you can do. Commented Feb 27, 2015 at 16:06

1 Answer 1

0

Found a fix to the problem

simply, place ob_end_flush(); at the top of the script, and Wordpress will no longer be able to catch the output of the script, and won't be able to do anything funny with it. It also seems to stop the page being cached, which is fine by me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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