MyBB Community Forums

Full Version: Word Counter Plugin - Getting Post ID
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm currently in the process of working on a word counter plugin for my forums. The premise is pretty simple - count the number of words in a post and add it to the new field (wordcount) in the posts table in the database.

I was wondering if there was a way to use the datahandler hooks (datahandler_post_insert_post_end and datahandler_post_insert_thread_post) to do this? I attempted it, but I ran into problems updating the table because at that point, there is no post ID and I wasn't sure how I would get that new post ID through those hooks.


I currently have it working using newreply_end and newthread_end, but I'm having trouble getting it to work with the fast reply and quick edit, so I'd like to use the datahandler hooks if it's at all possible.
So you want to populate an DB field upon inserting a post:
$plugins->add_hook('datahandler_post_insert_post', 'WordCounter::insert_post');
$plugins->add_hook('datahandler_post_insert_thread_post', 'WordCounter::insert_post');
$plugins->add_hook('datahandler_post_update', 'WordCounter::update_post');
class WordCounter
{
	function insert_post(&$dh)
	{
		if(!isset(isset($dh->message)))
		{
			return;
		}

		$word_count = (int)$this->__count($dh->message);

		if(isset($dh->post_insert_data))
		{
			$dh->post_insert_data['my_words_count'] = $word_count;
		}
		else // drafts, post edits, etc
		{
			$dh->post_update_data['my_words_count'] = $word_count;
		}
	}

	function update_post(&$dh)
	{
		$this->insert_post($dh);
	}
}

You will need to consider post moderation (splits, merges, etc).

EDIT: This is, assuming your DB field is in the posts table.
Thanks for the response! I should have mentioned that this plugin is for 1.6 since I haven't been able to upgrade my board yet (still working on updating the theme and waiting on a few other plugins). Sorry, that's my bad. >.<

Would this same idea work, though? Just add my field on to the $this->post_update_data array when I hook into those functions? If so, then I think I'll be able to work it out from here, I just wasn't sure if you could do that.
Except the three first lines everything else should work, yes. That wasn't changed between versions.