MyBB Community Forums

Full Version: Custom variable not working in hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have ran into an issue, and I haven't been able to resolve it myself. A quick search shows code similar to my own, but for some reason it just doesn't want to work for me. It's dead simple too.

I have a function that hooks into postbit. I have tried making this a custom variable, and also tried making it a key within the array. None of it wants to work.

This is my code:
$plugins->add_hook('postbit', 'add_to_postbit');

function add_to_postbit(&$post)
{

    $post['test'] = 'This is a TEST variable.';

}


The in the template I'm doing {$post['test']}, but it just doesn't output anything.

I have tried making it a custom variable $test in the add_to_postbit function, and also global'ing it inside that function, but it doesn't work either with the output {$test}.

Any ideas as to what I'm doing wrong?
In which template are you adding it? The postbit hook placement in inc/functions_post.php is inconvenient, it's added after many postbit_ templates get eval'd. So you have to get around it by inserting something like <!--replaceme--> to that template and then using str_replace() to change it to your code.
(2015-02-04, 09:29 PM)Destroy666 Wrote: [ -> ]In which template are you adding it? The postbit hook placement in inc/functions_post.php is inconvenient, it's added after many postbit_ templates get eval'd. So you have to get around it by inserting something like <!--replaceme--> to that template and then using str_replace() to change it to your code.

Would you be able to give an example of that in action?

Would it be something along the lines of this?

function add_to_postbit(&$post)
{

    global $post, $postbit, $templates;

    eval("\$postbit = \"".$templates->get("postbit")."\";");

    $postbit = str_replace('<!-- test -->', 'Testing 1,2,3', $postbit);
    
    return $postbit;
}
(2015-02-04, 10:01 PM)Stewartiee Wrote: [ -> ]Would it be something along the lines of this?

No. So you're adding it to the postbit template, yes? Shouldn't be a problem then. Can't tell more without seeing the exact code.
(2015-02-04, 10:11 PM)Destroy666 Wrote: [ -> ]
(2015-02-04, 10:01 PM)Stewartiee Wrote: [ -> ]Would it be something along the lines of this?

No. So you're adding it to the postbit template, yes? Shouldn't be a problem then. Can't tell more without seeing the exact code.

No, not the postbit it is the postbit_author_user where I'm trying to add the variable to. I just assumed I'd have to recreate the postbit template in that last code sample.
Well, I asked earlier to which template you're adding it for a reason..

Anyways, you need something like this then:
function add_to_postbit(&$post)
{
    $post['user_details'] = str_replace('<!-- test -->', 'Testing 1,2,3', $post['user_details']);
}
(2015-02-04, 10:27 PM)Destroy666 Wrote: [ -> ]Well, I asked earlier to which template you're adding it for a reason..

Anyways, you need something like this then:
function add_to_postbit(&$post)
{
    $post['user_details'] = str_replace('<!-- test -->', 'Testing 1,2,3', $post['user_details']);
}

Thank you for your help Smile