MyBB Community Forums

Full Version: Help using hooks for new post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I just installed myBB today and I chose it because it looked like the hooks system would do what I needed. I'll probably try the Hooks plugin rather than do a whole separate plugin. I'm trying to run some PHP code after every new post in a specific sub-forum. My code needs to export the post to another program.

At first I thought that I should use the hook "newreply_do_newreply_end" or "newreply_end" (BTW, what is the difference between them?) However, it looks like they would not pass any data to my code/plugin. Correct?

So maybe I should use "postbit" hook because it passes "$post" which I think is a data structure that includes the contents of the post and some other data such as the user that posted it. Correct?

Where can I find a definition of the $post data structure?

Thanks
The best hook would probably be datahandler_post_insert_post - it's called whenever a new post is inserted.

Take this code for example:

$plugins->add_hook('datahandler_post_insert_post', 'myfunc_name');
function myfunc_name(&$that)
{
    $fidToActOn = 1; // The fid to act on - in reality, we'd pull this from $mybb->settings...
    if ($that->post_insert_data['fid'] == $fidToActOn) {
        // Do our thing
    }
}

To see the structure of $that, you could just var_dump it or look in ./inc/datahandlers/post.php within the function"insert_post()".
(2013-01-10, 12:10 AM)Euan T. Wrote: [ -> ]The best hook would probably be datahandler_post_insert_post - it's called whenever a new post is inserted.


To see the structure of $that, you could just var_dump it or look in ./inc/datahandlers/post.php within the function"insert_post()".

Thanks, I think that put me on the right track.

This code works:
$myFile = "/var/www/mybbStuff/log.txt";
$fh = fopen($myFile, 'w');
$mystring = print_r($that, TRUE);
fwrite($fh, $mystring);
fclose($fh);

and it produces:
PostDataHandler Object
(
[language_file] => datahandler_post
[language_prefix] => postdata
[action] => post
[post_insert_data] => Array
(
[tid] => 1
[replyto] => 1
[fid] => 4
[subject] => RE: 12345
[icon] => 0
[uid] => 1
[username] => jstotz
[dateline] => 1357845653
[message] => 12th reply
[ipaddress] => 0
[longipaddress] => 0
[includesig] => 0
[smilieoff] => 0
[visible] => 1
[posthash] => 73fb886567a25f3122644bcd098ef20b
)

[post_update_data] => Array
(
)
-----cut-----

When I try to refer to individual elements it doesn't work. For example I tried:
$myFile = "/var/www/mybbStuff/log.txt";
$fh = fopen($myFile, 'w');
$mystring = print_r($that["post_insert_data"]["message"], TRUE);
fwrite($fh, $mystring);
fclose($fh);

That just hangs myBB and doesn't even complete posting the message. What is the correct syntax for accessing individual items in this data structure?
That's because it's an object. Try this:

$myFile = "/var/www/mybbStuff/log.txt";
$fh = fopen($myFile, 'w');
$mystring = $that->post_insert_data['message'];
fwrite($fh, $mystring);
fclose($fh); 
(2013-01-10, 08:04 PM)Euan T. Wrote: [ -> ]That's because it's an object. Try this:

$myFile = "/var/www/mybbStuff/log.txt";
$fh = fopen($myFile, 'w');
$mystring = $that->post_insert_data['message'];
fwrite($fh, $mystring);
fclose($fh); 

That worked, thanks.
Glad I could help Smile
I got my code working but it doesn't get run for the first post of a new thread. I would have thought that after the thread was created the same insert post code would be called, but it looks like insert thread uses an entirely separate code path and I need to add the same hook to "datahandler_post_insert_thread" or "datahandler_post_insert_thread_post". Does that sound right?
Yeah, I think that's right. I'm not entirely sure why it happens that way but obviously it does.
datahanlder_post_insert_thread i the one that handles a new thread/post iIRC
(2013-01-11, 03:12 PM)jstotz Wrote: [ -> ]I got my code working but it doesn't get run for the first post of a new thread. I would have thought that after the thread was created the same insert post code would be called, but it looks like insert thread uses an entirely separate code path and I need to add the same hook to "datahandler_post_insert_thread" or "datahandler_post_insert_thread_post". Does that sound right?

Depends on what fields you want to grab. You can use the datahandler_post_insert_thread if you want to grab the thread data, use datahandler_post_insert_thread_post if you want to grab the first post data (you can also use it for grabbing the thread data).
Pages: 1 2