PDA

View Full Version : hook for posting data?


orange gold
07-15-2011, 10:26 PM
I have coded alot in my days but I am new to VB and PHP.. Anyways I want a simple peice of code that will go through a users post right before they post it or preview it and replace string 'x' with string 'y'..

Lets say for the sake of exmaple it will detect the word "infinity" and replace it with the character "∞"

This is what I have so far (for the main part of the plugin) and I have no clue where to go? any guiding hands??


<plugin active="1" executionorder="5">
<title>Math</title>
<hookname>newpost_process</hookname>
<phpcode><![CDATA[if (THIS_SCRIPT == "index" and $vbulletin->options['math_onoff']) {
$mathpost .= '';
$template_hook['postbit_messagearea_start'] .= 'mathpost';
}]]></phpcode>
</plugin>

kh99
07-15-2011, 10:53 PM
I think you have the correct hook location, you'd want to do something like:

$post['message'] = str_replace("infinity", "∞", $post['message']);


or if you have more than one potential replacement:



$find = array("infinity", "pi");
$replace = array("∞", "pi symbol");
$post['message'] = str_replace($find, $replace, $post['message']);



and if you want it to be case insensitive, use str_ireplace() instead (same parameters).


BTW, I see what you posted is from an install xml file. If you're working on it that way you're probably finding it annoying. What you really should do is work on a test site in debug mode, then export the install xml when you're done.

orange gold
07-15-2011, 11:02 PM
Thanks! But how do I let it know not to post the original message then my message (with replacements) but only post the replacement message?

Here is my main section of the code for the product:


<plugins>
<plugin active="1" executionorder="5">
<title>Math</title>
<hookname>newpost_process</hookname>
<phpcode><![CDATA[if (THIS_SCRIPT == "index" and $vbulletin->options['math_onoff']) {
$find = array("infinty", "pi");
$replace = array("∞", "π");
$post['message'] = str_replace($find, $replace, $post['message'];
}]]></phpcode>
</plugin>
</plugins>

kh99
07-15-2011, 11:08 PM
It should post only the replacement, because what it does is changes the post text before it's saved.

By the way, I tried it out and found a couple typos in what I posted: infinity is spelled wrong and I left off a close paren at the end of the str_replace line.

orange gold
07-15-2011, 11:44 PM
Haha! good deal! Alright thanks! :)

Edit: just installed the product and when I went to make a post it didn't seem to work? Do I have the right hooks? :(

nhawk
07-16-2011, 12:24 PM
You have 1 problem with your code.

You're checking if the script name is 'index'. New posts are never posted via the index page.

orange gold
07-16-2011, 08:10 PM
ahh! Thanks! Silly me, Don't know why I left that in there! :o I got the section from another users product.xml file..

So now 1 last question.. It was supposed to replace the "infinity" with "∞" but instead it put a "?" I had to use HTML code "&#*8734;" (without the *asterix)

Does that have to do with how I'm compiling the xml file or is it just how the forum reads it?