w3it.org – Internet Helper Blog
  
www ITBlog Articles Topics Forums Main
 
    

Wordpress: feed output error solution.
How to remove white spaces and fix problems

The wordpress feed sometime return error and you can’t find out the way to solve the problem about white spaces output before the XML declaration, and that is responsible of the broken result.

I had the same problem in this online blog feed, while on my localhost LAMP test server all was correctly returned and the feed displayed without errors.

The common error is:

XML Parsing Error: XML or text declaration not at start of entity

Or, if you are validating a page using the w3 validator:

XML Parsing Error: XML declaration allowed only at the start of the document

This means that before the < character there are unwanted characters that need to be eliminated (also white spaces) for a clean and correct XML declaration.
<?xml version="1.0" encoding="UTF-8"?>
Thinking on how to solve the problem, and after many wrong reads all over internet the easy solution have come out in my simple mind. What we need, is to add a little tip in specific files where wordpress feeds are made up before to be outputted to the browser on request.
So, we go to fix this by opening the follow file, if the feed we want is RSS2 in wordpress (we will see all any other after in any case):

wp-includes/feed-rss2.php

open it with a text editor and read on the very top section, the following code:

header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
$more = 1;

To fix the feed on wordpress (but not only, it is obviously valid in, and for, any other feed or non feed contest where we can have the same necessity), add this tricky Php code immediately after:

$out = ob_get_contents();
$out = str_replace(array("\n", "\r", "\t", " "), "", $input);
ob_end_clean();

Do the same almost with the comment’s feed file: feed-rss2-comments.php
Save/replace the feed-rss2.php and the feed-rss2-comments.php files modified in this way and enjoy your re-enabled feeds!

see the fixed w3it.org blog feed

Note that not only the file feed-rss2.php on wordpress is able and used for feed purpose. In the same way, you may need to edit the following files if the requested feed is another:
feed-rss2-comments.php, feed-rss.php, feed-rdf.php, feed-atom.php, feed-atom-comments.php.

There is also another strange behavior where browsers fail with entities recognize and the comments feed of wordpress result corrupt or interrupt.
The common error is concerning the fact that in wordpress is possible to set permalinks (SEO) and in this case, sometime, the characters sequences in some points are recognized like broken entity.
For example a typical wordpress link result in the feed source will be:

http://www.axew3.com/b10g/?page_id=5&cpage=1#comment-357

The &cpage sequence is recognized in explorer as broken entity, it return error asking for the ; char ( entity &cpage; ) . Really there is no entities &cpage; for what i know, but the character & in the link let understand to the browser the wrong. The solution is to substitute exactly the character & with his respective entity. The correct returned link url in the feed source should be:

http://www.axew3.com/b10g/?page_id=5&amp;cpage=1#comment-357

So, where to edit fixing this problem for the wordpress comments feed returning error?
This is the tricky way to solve:
Open the wordpress file wp-includes/comment-template.php and search for this line contained inside the function get_comment_link():
return add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
substitute the above with this code:
return add_query_arg( 'amp;cpage', $args['page'], get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
Note the trick is this: we go to add exactly amp; and not &amp; as the char & has already been added in another function that contribute with this to build the link Url.
Save and overwrite the original comment-template.php file with the new edited: enjoy now your wordpress comment feed correctly parsed.