How to Fix

How to Fix "Invalid xml declaration" in WordPress

27.02.2012

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.
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:  */
 7:
 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,

Promote your business or products in our podcasts

We produce two podcasts: Burzcast and micON. Promote your business or products in our text or video content as well. Slots are limited to one advertiser per week!

Articole Burzcast

Postări din arhivă

Revolutionizing Industry: How AI is Transforming the Industrial Internet of Things

The Industrial Internet of Things (IIoT) revolutionizes how companies operate, allowing them to optimize processes, improve efficiency, and reduce costs. One of the key technologies driving this transformation is Artificial Intelligence (AI).

Search Engine Optimization, Search Engine Marketing, and Copywriting Services are Dead. Get Over It!

SEO and SEM, as well as copywriting as we know it, are becoming less effective and are likely to become obsolete shortly.

#BlackLivesMetter

I am sad and it is a shame to see that, the country that prides itself on being the land of all possibilities and a fair country has come down to this.

A New Chapter for Burzcast

The transition to the new brand is almost done. As we are closing the doors to an old chapter in our business, we are opening them to a new one.

Why You Should Never Start a Podcast

The other day I had an interesting conversation with an old friend who was quite keen to tell me that I'm spending my money and time in the wrong way by doing a weekly podcast.

The New Burzcast Website

We have decided to build this brand in a way in which it can prove beneficial for the whole company.