Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been working on a plugin for Wordpress, that updates an XML file with elements from the post. I have used a third party plugin to create the field for the XML, and so far that is working.

I am having a lot of trouble with the XML and the hook itself. I am using a DomDocument example from php4every1.com, and that works whenever I run it seperately in a php-file. However, if I include it within a function, that is hooked to the publishing of a post in wordpress, I get the following error: "The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin." and nothing more happens. The function including the XML will not run at all, and I have no idea why.

Here is my code:

<?php

add_action('publish_post','update_xml');

function update_xml(){

  $xml = new DOMDocument('1.0', 'utf-8'); 
  $xml->formatOutput = true; 
  $xml->preserveWhiteSpace = false; 
  $xml->load('fruits.xml');

  //Get item Element 
  $element = $xml->getElementsByTagName('item')->item(0);

  //Load child elements 
  $name = $element->getElementsByTagName('name')->item(0); 
  $price = $element->getElementsByTagName('price')->item(0); 
  $count = $element->getElementsByTagName('count')->item(0);

  //Change values 
  $name->nodeValue = 'Orange'; 
  $price->nodeValue = 14.33;

  //Create new element 
  $newCount = $xml->createElement('count', 10);

  //Replace old elements with new 
  $element->replaceChild($name, $name); 
  $element->replaceChild($price, $price); 
  $element->replaceChild($newCount, $count);

  //Create one new item element 
  $newItem = $xml->createElement('item'); 
  $newItem->appendChild($xml->createElement('name', 'hest')); 
  $newItem->appendChild($xml->createElement('price', 10.59)); 
  $newItem->appendChild($xml->createElement('count', 131));

  //Add it to fruits element 
  $xml->getElementsByTagName('fruits')->item(0)->appendChild($newItem); 
  $xml->save('fruits.xml');

}

?>

and the xml file:

<?xml version="1.0" encoding="UTF-8"?> <fruits>

<item>
    <name>Apple</name>
    <price>10.25</price>
    <count>15</count>
</item>

</fruits>

As I said, the above works whenever I am running it in a seperate php-file without the hook and the function, but inside the hook in the plugin, it breaks. I am pretty new at programming, php and wordpress, so I bet I am overlooking something silly, but what?

Thanks in advance.

Edit: I get this error when activating the plugin: "The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin."

share|improve this question
    
will not run at all - are you completely sure about this? It might be running, but not doing what you think it should. That would be entirely different issue. –  Rarst Sep 7 '11 at 22:14
    
@Rarst: pretty much. The XML code works on its own, but inside the function it doesn't. Anything outside works - I can debug with echo there, but nothing happens inside. Also, I get this error when activating the plugin:"The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin." –  Spamshield Sep 7 '11 at 22:36
add comment

2 Answers

up vote 1 down vote accepted

The 1 character output error is probably because you have a single space before <?php or after ?>.

As far as it not doing anything, where is the fruits.xml file? this line:

$xml->load('fruits.xml');

in this context is looking for the file at:

/your/server/path/to/wp-admin/fruits.xml

Which I assume is not what you're expecting. Have a look at Determining Plugin and Content Directories in Codex.

share|improve this answer
    
This worked, thanks! There was an extra line at the end, and placing the xml-file in wp-admin made it run smoothly. I can't really figure out the correct way to link to the supposed file location (within the plugin directory), even though I have gone through your link. –  Spamshield Sep 8 '11 at 21:17
    
Solved that too. Used $xml->load(WP_PLUGIN_DIR . '/XMLTest/fruits.xml'); to point it at the correct directory. Now I just need to figure out how to append the new elements in the beginning of the XML-file. –  Spamshield Sep 9 '11 at 12:09
add comment

To extend @Milo answer slightly: The "headers already sent" message can be caused by echo statements in eg. register_activation_hook() functions.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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