MyBB Community Forums

Full Version: Hook in format_name()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm trying to hook into format_name() and influence the output $parameters['format'], which in itself is going smoothly, but when trying to modify based on another key within $parameters (for example username), it doesn't work at all. For example:

if($parameters['username'] == 'whatever')
{
 $parameters['format'] == '<strong>{username}</strong>';
}

return $parameters;

It appears no other key of $parameters is passing into the hook. Looking at functions.php it appears that this should work. I'm just mystified by this, what use is this if all I can do is broadly modify format without any conditions?

EDIT: So nothing in $parameters is passing into the hook and I'm just outputting a modified format. I guess I don't know how to pull any information in from the parent function, and global won't work unless I apply it to functions.php and that I do not want to do.
I am not sure I got you exactly but at a glance '<strong>{username}</strong>' should be '<strong>'.$username.'<strong>'
Hey, thanks for the reply. $parameters['format'] appears to be looking for HTML formatting with a {username} token that it preforms str_replace upon. The parent function format_name() pulls $username and sets it as $parameters['username'], but does not use it and does not pass it. It appears that the hook in format_name() is for overwriting $parameters and I'm trying to overwrite it on a per username basis.

I'm starting to think that this hook isn't enough to do what I want and that this is more work than I want it to be.
You can take a look at this under dev plugin to have an idea how I have overridden username style
https://github.com/mybbgroup/Bauble-name
unless it's just a typo in your forum post, you are making a == comparison instead of an = assignment inside the if block
(2019-04-20, 12:04 PM)effone Wrote: [ -> ]You can take a look at this under dev plugin to have an idea how I have overridden username style
https://github.com/mybbgroup/Bauble-name
This looks pretty nifty, thanks. Having a look at it now.

(2019-04-20, 01:50 PM)frostschutz Wrote: [ -> ]unless it's just a typo in your forum post, you are making a == comparison instead of an = assignment inside the if block
Yeah just a typo. I have no problem overwriting the parameters, I just can't compare them.
so what happens if you print_r($parameters) inside your hook function?

// replace with any flavor of debugging you prefer
echo '<pre>'.htmlspecialchars(print_r($parameters, true)).'</pre>';

I've never used this hook myself but from the source code, the only odd thing is it's only called once per username then the result is cached. So you can't do a random color cycle or something like that (same username random colors within a page). Or at least, you can't with the hook alone.
(2019-04-20, 10:44 PM)frostschutz Wrote: [ -> ]so what happens if you print_r($parameters) inside your hook function?

// replace with any flavor of debugging you prefer
echo '<pre>'.htmlspecialchars(print_r($parameters, true)).'</pre>';
The only problem with that seems to be that when printing anything inside the function causes a "Content Encoding Error" in the browser. gzip explodes, but turned off: doesn't print anything. I tried something like this before and wasn't getting anything.

I think I just misunderstood the hook's purpose, though I'm still not sure what that is. Only $parameters['format'] is used in the parent function, so you can do a global override of username format. Other than that, I suppose you could attach a bigger blob of code to it, but it doesn't seem like good idea.
the original function makes a decision based on the usergroup/displaygroup namestyle setting (if present) otherwise just plain username.

the hook should have access to: the plain username, the usergroup/displaygroup, and the format decision made by the original function.

so you can do (almost*) all the original function can do and make your own decision instead.

*(unfortunately original function sets usergroup to displaygroup so given a displaygroup, the original usergroup is lost to the hook)



if embedding debug message in output does not work, write it to a file instead. in any case, find a way to make debug messages work. no way to develop anything without a debug facility
(2019-04-20, 11:20 PM)frostschutz Wrote: [ -> ]the hook should have access to: the plain username, the usergroup/displaygroup, and the format decision made by the original function.
I'm not able to use $username or $parameter['username'] to make any comparisons. Am I missing something?
Pages: 1 2