Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For plugin files, does a php file have to be ended with ?>? I'm asking this because I'm getting the error, XML Parsing Error: XML or text declaration not at start of entity and found this post: http://wordpress.org/support/topic/no-rss-xml-parsing-error, http://wordpress.org/support/topic/xml-parsing-error-xml-or-text-declaration-not-at-start-of-entity-2, suggesting to check all files and remove any blank lines at the top or bottom.

I have a plugin file which does not end with ?>. Something like,

<?php 
     .... some code here ...
?>
<html>
<body></body>
</html>

So should this be changed to this?

<?php 
     .... some code here ...
?>
<html>
<body></body>
</html>
<?php ?>

I found that even wp-config-sample.php is not ending with ?> in v3.4.1.

share|improve this question
1  
No, such a construct is never needed. You can even omit regular closing tags if there is no output after the last line of PHP code. See basic syntax. – toscho Sep 1 '12 at 12:37
Can't believe... it's not off topic at all. See the official forum topics I linked. It say to ensure to close the file with ?> – Teno Sep 1 '12 at 12:38
Basic PHP syntax questions are on topic on Stack Overflow – where this question has been answered already numerous times. This site is about WordPress specific questions only. – toscho Sep 1 '12 at 12:41
The XML Parsing Error is WordPress specific. Please READ the question carefully. – Teno Sep 1 '12 at 12:43
Your question is about the PHP closing tags. If you want to solve the XML error, add the (first lines of the) code of the XML file, and rewrite your question. Flag it for moderator attention then, and we will see if it is on topic. – toscho Sep 1 '12 at 12:49
show 1 more comment

closed as off topic by Chip Bennett, Brian Fegter, Wyck, Rarst Sep 9 '12 at 21:50

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

Your Question

For plugin files, does a php file have to be ended with ?>?

It depends. The closing ?> tag just tells the PHP parser that the file in finished. In some cases, closing a file with this tag can cause problems because you'll end up with additional whitespace after the closing tag. This extra whitespace can cause the PHP parser to throw errors.

(Here's an example from an older support thread ...)

The Error

No, this is not a WordPress-specific error.

The error is triggered by the XML parser internal to PHP itself - WordPress merely uses this parser to read RSS files. It looks like you have a plugin adding a blank line somewhere before the opening XML declaration. This is the exact same issue that arose in both of the support threads you linked:

Did view source and <?xml etc is the second line first line is blank

So to answer your question: No, plugin files do not have to end with ?>. The error you're getting is being caused by something else entirely.

If you want help debugging it, please ask a new question. Give us the code that's triggering the error (or tell us how you're trying to load a file) and provide us with the XML that you're trying to parse as well. We can't debug blind.

share|improve this answer
It looks like you have a plugin adding a blank line somewhere before the opening XML declaration. This is the exact same issue that arose in both of the support threads you linked: Then it is an issue relating to WordPress, especially in a topic of developing WordPress plugins. Also, I've recently started having "headers already sent" errors. And as I researched the issue, many pages mention that the plugin format must end with ?>. – Teno Sep 4 '12 at 19:04
See wordpress.stackexchange.com/questions/16547/… codex.wordpress.org/… Check that the very last characters are ?> It explicitly states the file must end with ?>. – Teno Sep 4 '12 at 19:08
Although this question has arisen due to the XML error, it does not mean to be asking to debug the plugin script causing that error. The question is simply asking whether the formatting is right or not. – Teno Sep 4 '12 at 19:22
1  
That depends entirely on the plugin. Not all plugins need to end with a ?>. You're getting an error because some code somewhere is trying to parse an incorrectly formatted file as XML. The "headers already sent" error is caused by include()ing a php file that's incorrectly closed (i.e. whitespace after the final ?> tag). Neither of these are a WordPress issue, they will arise in any PHP-based application because they are PHP parser errors. – EAMann Sep 4 '12 at 19:49
2  
In the future, if a question is closed in error, take the time to edit your question to add extra detail. Your original question appeared entirely off-topic and, had Toscho not closed it, I probably would have. It's not an attempt to limit you at all, it's an attempt keep the site clean and free from multiple off-topic and potentially distracting questions. – EAMann Sep 4 '12 at 23:14
show 6 more comments

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