Problems with creating hook that updates XML

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.”

2 Answers
2

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.

Leave a Comment