Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing and changing content when a page is saved
#1
Hi all, I'm new to GetSimple (I love it) and I'm curious: is there a way of intercepting the content when a user saves a page, without hacking changedata.php ?

I made a textile plugin that converts textile to HTML on the fly, but my first instinct was to try to do the conversion when the page is saved, as there's no reason in principle why it should be done when the page is requested.

I see the action changedata-save, but I can't see how to manipulate the content - maybe I haven't looked hard enough.
Reply
#2
You can just use the changedata-save hook for this. Above that one the following code has been used:
Code:
$note = $xml->addChild('content');
        $note->addCData(@$content);
You’re going to want your plugin to grab $content, run your filter, and change the content child of the SimpleXML object to your own output.

That’s all, when your plugin has changed the SimpleXML object it will get saved to the file because changedata-save runs before the actual XML-file creation.
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#3
Zegnåt Wrote:...and change the content child of the SimpleXML object to your own output

How?

Let's say I want to replace any "Hi!" in the content to "Hello!"
I've tried:
Code:
$xml->content = str_replace('Hi!','Hello!',@$content);
but then the content in the xml file does not have "<![CDATA[" and "]]>"
Reply
#4
I couldn't get that to work, but I'm not too worried: as I say, I've got the plugin running when the pages are displayed, like so...

Code:
add_filter('content','html_from_textile');

# functions
function html_from_textile($content) {
    $textile = new Textile;
    $html = $textile->TextileThis(stripslashes($content));
    return $html;
}

...which works fine.

I'll need to learn more about how GetSimple is putting things together to get it working the other way.
Reply
#5
Carlos Wrote:[…] then the content in the xml file does not have "<![CDATA[" and "]]>"
No, because you never tell it about that. You just select the content element in the SimpleXML object and put a string in it. SimpleXML has no standard way of adding CDATA blocks. That’s why Chris made sure there was an addCData-method.

Either you’ll want to use the addCData-method or something like this:
Code:
$xml->content = "<![CDATA[".str_replace('Hi!','Hello!',@$content)."]]>";
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#6
Thank you for the explanation, Zegnåt.

Zegnåt Wrote:Either you’ll want to use the addCData-method or something like this:
Code:
$xml->content = "<![CDATA[".str_replace('Hi!','Hello!',@$content)."]]>";

I had tried that, but didn't work (it is saved as "&lt;![CDATA....")

I've done some tests, this is a sample plugin that does the trick. I don't know if it should have been done other way ("$xml=..." lines), what do you think?

Code:
<?php
$thisfile = basename(__FILE__, ".php");
register_plugin(    $thisfile,'filtercontenttest','','','','');

add_action('changedata-save','filtercontenttest');

function filtercontenttest() {
    global $xml;
    global $content;
    
    $xml->content='';
    $xml->content->addCData(str_replace('Hi!','Hello!',@$content));
}
?>

Anyway for doing any $content filtering without fiddling with xml things, maybe it would be better to have another hook ('changedata-beforesave' or something like that) somewhere before:

Code:
$xml = @new SimpleXMLExtended(....
Reply
#7
Ah, I see. Seems obvious now.

And yeah - having a hook before the xml seems to make a lot of sense.
Reply




Users browsing this thread: 1 Guest(s)