MyBB Community Forums

Full Version: Using showthread_end hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, so I'm trying to play around with hooks to get a good idea of how it works, and at my first attempt I'm already meeting a bit of a hazzle.

The plugin contains the default setup and then this code:

$plugins->add_hook("tcd_evaluate", "showthread_end");

(...)

function tcd_evaluate()
{  
    if ($forumpermissions['canpostreplys'] != 0 && $thread['closed'] == 1) {
        eval("\$newreply = \""."Text Here?".$templates->get("showthread_newreply")."\";");
    }
}


I'm trying to intervene before the showthread page is output, to write text besides the 'new reply' button, and the proposition is pretty much just saying "If user can post and thread is closed", so that only I can see it.

My skills at PHP is decaying day for day, and I'm not sure how to go about this, is my logic in how PHP functions wrong? xD

I'd be really glad for any help, thanks a lot.
I'm not familiar with this particular hook, but one thing that could cause this problem is that the newreply variable isn't being used in global scope, which means that PHP is evaluating a new $newreply variable that is only visible within this particular function. Try putting this at the very beginning of the function and see if this resolves the issue?

global $newreply; 

Edit: Also, the hook itself is passing the parameters in the wrong order. Switch it to:

$plugins->add_hook("showthread_end", "tcd_evaluate");
Thanks Darth for the help, I realize that you're right about the order of passing parameters to the add_hook function, thanks for that Smile

As for the globalization of the newreply variable, it doesn't fix it for me either, sadly. I have been looking at documentation, and the two variables, newthread and newreply, are initialized together and in the same scope as the hook is run, so I should be able to access the variable now but it still is not working hmm.

I'm not sure what else could be the problem, for the sake of new posts the updated code is here:

$plugins->add_hook("showthread_end", "tcd_evaluate");

function tcd_evaluate()
{   
    global $newreply;

    if ($forumpermissions['canpostreplys'] != 0 && $thread['closed'] == 1) {
        eval("\$newreply = \""."Text Here?".$templates->get("showthread_newreply")."\";");
    }
}

If it is still not clear what my intention is, it is to add something to the $newreply variable through hooks, so that I change the content of newreply, before newthread is used to output a page. Newthread consists of many templates, where newreply is included. 

Thanks again for the help, I really do appreciate all and any input.

---

Should be said that other than declaring newreply as global, I did also try using it as a function parameter, in the hope that it would work returning it.

Like this:
function tcd_evaluate($newreply)
{   
    if ($forumpermissions['canpostreplys'] != 0 && $thread['closed'] == 1) {
        eval("\$newreply = \""."Text Here?".$templates->get("showthread_newreply")."\";");
    }

    return $newreply;
}

I ended up fixing it, if there is any interest here, obviously I could find a few mistakes looking at it again today.

1. $templates should be declared a global variable. And so should newreply.
2. My if statement was never fulfilled.

For some reason, with this code:

function tcd_evaluate()
{   
    global $templates, $newreply;

    eval("\$newreply = \""."Text Here?".$templates->get("showthread_newreply")."\";");
}

I get this result:
[Image: AKqCT8e.png]

The text location is at the right place, lucky for me I chose the right hook and variable haha. Unlucky for me, as you can see, the button is also missing.

Okay I fixed it for real now, sorry for all this orange xD I'm just a stupid non-thinker for reals.

Obviously I neglected the variables that the newreply template is using.
(2020-03-21, 11:57 PM)GodLess101 Wrote: [ -> ]Obviously I neglected the variables that the newreply template is using.

Your if statement was never fulfilled because you need to globalize $forumpermissions and $thread as well.