I'm trying to catch hashtags in posts. The problem is when I use parse_message_end, it doesn't actually override them.
But when I use parse_message it does, but it also messes up special characters because at that point they are escaped and have their #blah id in place.
Paul H. had used parse_message_end in his plugin a while back for MyBB 1.6, but that method doesn't seem to work in 1.8.
What would be the correct place to hook in?
I just tried and it seems to work fine for me. Can't say more without seeing your code..
The code is Paul H's old MyTags plugin with some modifications I made to try and make it work with MyBB 1.8 as well as support foreign characters and not just English ones (since I saw someone complaining about that).
<?php
/*
Copyright 2014 PJGIH & JMPH
See license here: http://opensource.org/licenses/MIT
*/
if(!defined("IN_MYBB")) {
die("This file cannot be accessed directly.");
}
//Add Your Hooks
$plugins->add_hook("parse_message_end", "tag_parse");
//REQUIRED
function mytags_info()
{
return array(
'name' => 'MyTags',
'description' => 'Uses # to make clickable and searchable hashtags, like on twitter.',
'website' => 'http://www.mybb.com',
'author' => 'PJGIH & JMPH',
'version' => '1.1',
"compatibility" => "18*"
);
}
function mytags_activate() {
//no settings
}
function mytags_deactivate()
{
//no settings
}
function tag_parse($message) {
$tweet = $message;
$message = preg_replace("/(#S+)/i",
'
<form method="post" action="./search.php" style="display:inline;">
<input type="hidden" name="action" value="do_search" />
<input type="hidden" name="postthread" value="1" />
<input type="hidden" name="forums" value="all" />
<input type="hidden" name="showresults" value="posts" />
<input type="hidden" name="keywords" value="$1" />
<a href="#" style="display:inline; onclick="document.forms[0].submit()">$1</a>
</form>
',
$tweet);
return $message;
}
?>
Well, found 3 mistakes so far:
1. The regex didn't really look correct for me and I was too lazy to write another one myself, so I replaced it with a random find from stackoverflow:
http://stackoverflow.com/a/16942496 Now it's parsed.
2. Umm..
style="display:inline;
Incorrect HTML syntax.
3. What's this?
document.forms[0].submit()
It would submit the 1st form on the page afaik. Replaced it with other JS code.
So here's my function:
$plugins->add_hook("parse_message_end", "tag_parse");
function tag_parse($message)
{
global $mybb;
$repl = <<<EOF
<form method="post" action="{$mybb->settings['bburl']}/search.php" style="display: inline;">
<input type="hidden" name="action" value="do_search" />
<input type="hidden" name="postthread" value="1" />
<input type="hidden" name="forums" value="all" />
<input type="hidden" name="showresults" value="posts" />
<input type="hidden" name="keywords" value="$1" />
<a href="javascript:void(0)" onclick="$(this).closest('form').submit()" style="display: inline;">$1</a>
</form>
EOF;
$tweet = $message;
$message = preg_replace("/#([^#]+)[\s,;]*/i", $repl, $tweet);
return $message;
}
Demo:
http://noo.hol.es/mybb18/showthread.php?tid=10
http://noo.hol.es/mybb18/showthread.php?tid=11
$message should be passed by reference instead though, just FYI.
Thanks guys, that helps a lot!