Razvan Burz

de Razvan Burz

How to Fix "Invalid xml declaration" in WordPress

It’s Monday morning and we already have a few support tickets up. First one is related to our website, at Dev-Vision.com. As you may already know, our website runs on WordPress and we like this CMS because it’s easy to use, easy to develop on it and easy to customize and expend. The issue we are facing here is related to our RSS feed which for some reason doesn’t work. Sometimes WordPress does that and apparently no one can find out why this is happening. When one is trying to access the RSS feed a new line character is inserted at the beginning (1-st line) of the XML file thus resulting an invalid XML file which many RSS feed readers, including Google’s FeedBurner, can’t read properly.

What’s Wrong?

The problem is quite easy to resolve if you are a programmer who considers the unpredictable and any RSS reader developer should consider this. The fast and dirty fix is to TRIM the resulting feed (XML file) and that’s it! The problem is resolved. In a few minutes I added this fix to the resulting feed on our WordPress file.

How To Fix This for WordPress?

The file in charge of creating the content is feed.php but the file which loads the header is a small file located in the WordPress root folder, /wp-blog-header.php. This file has a line which calls the required file and adds the header to that. See the below line:
 1: require_once( dirname(__FILE__) . '/wp-load.php' );
Now, I don’t care where the XML file gets the extra line, which cases the feed to become invalid, but I’m using a simple function from PHP, ob_start. The PHP.net website is quite informative about the function:
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
Now, how about a few modifications to the wp-blog-header.php file?

Steps to Follow

First, do a local back-up of the file. It’s located in the WordPress installation root folder, so it should be quite easy to locate it. Now, open the file and let’s write some more lines above and below the line presented above (commented in the code bellow with “this is the line”):
 1: function callback($buffer)
 2: {
 3: return (trim($buffer));
 4: }
 5: ob_start("callback");
 6: require_once( dirname(__FILE__) . '/wp-load.php' ); //this is the line
 7: ob_end_clean();
I have inserted the lines just after the $wp_did_header = true; boolean variable and right above the wp(); function. My WordPress 3.3.1 wp-blog-header.php file looks like this:
 1: <?php
 2: /**
 3:  * Loads the WordPress environment and template.
 4:  *
 5:  * @package WordPress
 6:  */
 8: if ( !isset($wp_did_header) ) {
 9: $wp_did_header = true;
 10: function callback($buffer)
 11: {
 12: return (trim($buffer));
 13: }
 14: ob_start("callback");
 15: require_once( dirname(__FILE__) . '/wp-load.php' );
 16: ob_end_clean();
 17: wp();
 18: require_once( ABSPATH . WPINC . '/template-loader.php' );
 19: }
 20: ?>

What I’ve Done?

I’m saving the output RSS file into a buffer, so I’m only getting the header. The callback function is returning a trimmed string, which is the buffer – the main XML file for the RSS feed. By trimming the XML file I’ve got rid of the useless new line at the beginning of the file, thus the error is fixed. Something similar is doing the fix-rss-feed plug-in, which is quite useful, but not completely fixing the error sometimes. So, if you need the fixed wp-blog-header.php file, you may do the alteration yourself or you may download the archived file here. Please be advised that this file may only work for some of you and may stop your WordPress site from working correctly. Before overwriting or changing anything in the WordPress core files, please make a back-up. Hopefully, sometimes in the future, the WordPress team will find a fix for this. Until then, I’m hoping this fix will help you. All the best,
Ultimele două articole
Razvan Burz

Despre Razvan Burz

Salut, sunt Răzvan Burz: programator cu o vechime de peste 15 ani în software și web development, antreprenor, tată, soț, consultant pentru companii din multiple țări, podcaster (de peste 11 ani), blogger (de peste 12 ani), gamer ocazional și pasionat de cafea.

Poți obține conținut audio, video și text adițional exclusiv prin Burzcast™ VIP

Fă parte din comunitatea VIP, împreună cu alte sute de români minunați!

Abonează-te la newsletter!